Zidance Zidance - 3 years ago 167
PHP Question

How to increment the letter of alphabet by 2 or more instead of 1 each time?

I want to increment a letter by 2 each time, e.g.

// increment by 1
$alphabet = "A";
$alphabet++; // 'B'

I want something like

// increment by 2
$alphabet = "A";
$alphabet+=2; // 'C'

How can i do this? I tried the code above but encounter non numeric value.

Answer Source

Use php chr and php ord

$alphabet = "A";
$ascii = ord($alphabet);
$ascii += 2;
echo $alphabet = chr($ascii);

LIve demo : https://eval.in/907132

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download