Home >Backend Development >PHP Tutorial >PHP string increment and decrement example introduction_PHP tutorial

PHP string increment and decrement example introduction_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:39:10841browse

Today I saw this passage in the PHP manual:

"When dealing with arithmetic operations on character variables, PHP follows Perl's habits rather than C's. For example, in Perl $a = 'Z' ; $a++; will turn $a into 'AA', and in C, a = 'Z'; a++; will turn a into '[' (the ASCII value of 'Z' is 90, and the ASCII value of '[' The value is 91). Note that character variables can only be incremented, not decremented, and only support pure letters (a-z and A-Z). Incrementing/decrementing other character variables is invalid, and the original string will not change. "

That is. Say:

Copy code The code is as follows:

for($i = 'A'; $i <= 'Z '; $i++) {
echo $i;
//if( $i == 'ZZZ') die();
}

The result is: ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUA… ………

There are also string variables that cannot be decremented:
Copy code The code is as follows:

$ a = 'Z';
--$a;
echo $a; // Z

This also shows that $a++ or ++$a cannot be $a = $a + 1; to explain
Copy code The code is as follows:

$a = $b = 'Z';
$a = $a + 1;
echo $a; //1
++$b;
echo $b; //AA

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/733056.htmlTechArticleToday I saw this passage in the PHP manual: "When dealing with arithmetic operations on character variables, PHP follows the Perl convention, not C convention. For example, in Perl $a = 'Z'; $a++; will treat...
Statement:
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