Home > Backend Development > PHP Tutorial > js intercepts string increment operation code for string under PHP

js intercepts string increment operation code for string under PHP

WBOY
Release: 2016-07-29 08:43:40
Original
945 people have browsed it

A classmate asked a question:

Copy the code The code is as follows:


for($i = 'A'; $i <= 'Z'; $i++) {
echo $i;
}
//What is the output?


The output is:

Copy the code The code is as follows:


ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARAS…….


Why?
It’s actually very simple, PHP manual There are instructions in it, but I'm afraid many people won't read the manual carefully chapter by chapter:

Copy the code The code is as follows:


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 'AA', 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 dealing with arithmetic operations on character variables, PHP follows the habits of Perl, not C of. 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, not decremented, and only pure letters (a-z and A-Z) are supported.
That is, if:

Copy the code The code is as follows:


$name = "laruence";
++$name; //will be "laruencf"


And:

Copy the code The code is as follows:


$name = "laruence";
--$name; //No effect, still "laruence"


So, the cause of this problem is when $i = Z , ++$i becomes AA, and when comparing strings,
AA, BB, XX to YZ are all less than or equal to Z... so..
Author: laruence

The above introduces the incremental operation code for strings under PHP with js intercepting strings, including the content of js intercepting strings. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template