PHP analysis of string incrementing operations _ php tips-php Tutorial

Source: Internet
Author: User
When processing the arithmetic operation of character variables, PHP follows the Perl habit, not C. Some people asked a question:

The code is as follows:


For ($ I = 'a'; $ I <= 'Z'; $ I ++ ){
Echo $ I;
}


What is output?

The output is:

ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARAS .......

Why?

In fact, it is very simple. it is also explained in the PHP manual, but I am afraid many people will not read the manual carefully in one chapter:

PHP follows Perl's convention when dealing with arithmetic operations on character variables and not C's. for example, in Perl 'Z' + 1 turns into 'A', while in C 'Z' + 1 turns into '[' (ord ('Z') = 90, ord ('[') = 91 ). note that character variables can be incremented but not decremented and even so only plain ASCII characters (a-z and A-Z) are supported.

When processing the arithmetic operation of character variables, PHP follows the Perl habit, not C. For example, in Perl, 'Z' + 1 will get 'A', and in C, 'Z' + 1 will get '[' (ord ('Z ') = 90, ord ('[') = 91 ). Note that character variables can only increase, cannot decrease, and only letters (a-z and A-Z) are supported ).

That is to say, if:

The code is as follows:


$ Name = "laruence ";
+ + $ Name; // it will be "laruencf"


And:

The code is as follows:


$ Name = "laruence ";
-- $ Name; // no effect, or "laruence"


Therefore, the cause of this problem is that when $ I = Z, ++ $ I becomes AA, and if the string is compared,
AA, BB, XX until YZ are all less than or equal to Z... So ..

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.