Home > Backend Development > PHP Tutorial > PHP字符串的递增和递减示例介绍_PHP

PHP字符串的递增和递减示例介绍_PHP

WBOY
Release: 2016-06-01 11:57:17
Original
1431 people have browsed it

今天看到php手册上有这么一段话:

“在处理字符变量的算数运算时,PHP 沿袭了 Perl 的习惯,而非 C 的。例如,在 Perl 中 $a = 'Z'; $a++; 将把 $a 变成'AA',而在 C 中,a = 'Z'; a++; 将把 a 变成 '['('Z' 的 ASCII 值是 90,'[' 的 ASCII 值是 91)。注意字符变量只能递增,不能递减,并且只支持纯字母(a-z 和 A-Z)。递增/递减其他字符变量则无效,原字符串没有变化。”

也就是说:
复制代码 代码如下:
for($i = 'A'; $i echo $i;
//if( $i == 'ZZZ') die();
}

结果是:ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUA…………

还有字符串变量不能递减:
复制代码 代码如下:
$a = 'Z';
--$a;
echo $a; // Z

这也说明了$a++或++$a,不能要 $a = $a + 1;来解释
复制代码 代码如下:
$a = $b = 'Z';
$a = $a + 1;
echo $a; //1
++$b;
echo $b; //AA

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