PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<while 语法错误消息已经改变>
Last updated: Mon, 16 Jul 2012

表达式类型

PHP/FI 2.0 根据表达式左边判断表达式的类型,而 PHP 3.0 则根据表达式两边进行判断。这便可能导致 2.0 下运行正常的脚本在 3.0 下导致异常。

考虑以下例子:

$a[0]=5;
$a[1]=7;

$key = key($a);
while ("" != $key) {
   echo "$keyn";
   next($a);
}

在 PHP/FI 2.0 中,这段程序将显示所有 $a 的索引,而在 PHP 3.0 中,这个程序什么都不会现实。原因是,在 PHP 2.0 中,因为左边参数的类型是 string,于是,一个 string 的关系便建立起来。当然 "" 并不等于 "0",循环从头到尾。而在 PHP 3.0 中,当一个 string 和一个 integer 比较时,将 string 转换为 integer 并比较。结果是比较值为 0atoi(""),和同样是值为 0variablelist,也就是 0==0。所以循环根本没有执行。

很容易修正它。把 while 声明替换为:

while ((string)$key != "") {




<while 语法错误消息已经改变>
 Last updated: Mon, 16 Jul 2012
 
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: http://manual.phpv.net/
Last updated: Thu Jul 7 19:13:47 2005 CST