string constants analysis of increment arithmetic of PHP for string

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.