Why can you increment characters in php

In PHP, you can increase a character like this:

$b = 'a'++;

      

What I'm wondering is, in terms of language, why does this work? Is php interpreting the character in the same way as the ASCII value, so increasing it will just make the ASCII value 1 higher, which will be the next letter in the alphabet?

+3


source to share


2 answers


Check it out: http://php.net/manual/en/language.operators.increment.php



PHP follows the Perl convention when dealing with arithmetic operations on character variables, not C.

For example, in PHP and Perl $ a = 'Z'; $ A ++; turns $ a into 'AA', and into C a = 'Z'; A ++; converts a to '[' (ASCII value 'Z' is 90, ASCII value '[' is 91).

Note that character variables can grow but not shrink and therefore only simple alphabets and ASCII numbers (az, AZ and 0-9) are supported. Incrementing / decreasing other symbolic variables has no effect, the original string is not changed.

+3


source


Q: What I'm interested in, in terms of language, why does this work?

A: Why is English written from left to right and Arabic from right to left? It was the decision of the people who developed the language. They may have their own reason for this. I firmly believe this was by design, as if you were doing arithmetic addition ($ a + 1), it doesn't work the same way.



$a = 'a';

echo $a++; // output: 'a' , a is echoed and then incremented to 'b'
echo ++$a; // output: 'c' , $a is already 'b' and incremented first and then becomes 'c'

echo $a+1; // output: '1' , When in an arathamatic operation a string value is considered 0

// in short '++' seems to have been given special intrepretation with string
// which is equivalent to
echo $a = chr(ord($a)+1); // output: 'd' , the ascii value is incremented

      

Thanks for the question, I didn't know that "++" applies to strings.

0


source







All Articles