A classmate asked a question:
Copy the Code code as follows:
for ($i = ' A '; $i <= ' Z '; $i + +) {
echo $i;
}
What is the output?
The output is:
Abcdefghijklmnopqrstuvwxyzaaabacadaeafagahaiajakalamanaoapaqaras .....
Why?
In fact, it is very simple, the PHP manual is also explained, but I am afraid that a lot of people will not be a chapter of the manual carefully read it again:
PHP follows Perl ' s convention when dealing with arithmetic operations on character variables and not C ' s. For example, in Perl ' Z ' +1 turns to ' AA ', while in C ' Z ' +1 turns into ' [' (Ord (' z ') = = +, ord (' [') = = 91). Note that character variables can is incremented but not decremented and even so only plain ASCII characters (A-Z and A-Z) is supported.
When dealing with arithmetic operations of character variables, PHP inherits the Perl habit, not the C. For example, in Perl ' Z ' +1 will get ' AA ', while in C, ' Z ' +1 will get ' [' (Ord (' z ') = = 90,ord (' [') = = 91). Note that character variables can only be incremented, cannot be decremented, and only plain letters (A-Z and A-Z) are supported.
In other words, if:
Copy the Code code as follows:
$name = "Laruence";
+ + $name; It's going to be "LARUENCF."
and
Copy the Code code as follows:
$name = "Laruence";
-$name; No impact, or "laruence"
So, the reason for this is that when $i = Z, the + + $i becomes AA, and the string comparison,
Aa,bb,xx until YZ is less than or equal to Z ... so..
The above describes the string constant PHP to the string of the increment operation analysis, including the string constants of the content, I hope the PHP tutorial interested in a friend helpful.