empty("0")

可能最具争议的改变是对 empty() 函数行为的改变。一个仅包含字符“0”(是零,不是英文字母“O”)的字符串现在被认为是空字符串,而 PHP 3 中则相反。

这个改变对 web 程序很有意义,所有的 input 表单返回的都是 string,甚至填写的是数字。PHP 能够自动进行类型转换。但另一方面,这可能不是代码的原意,可能导致代码莫名其妙的错误。


add a note add a note User Contributed Notes
alan at frostick dot de
03-Dec-2001 02:01
The changes to empty() also affect isset() in a similar way.

Under php3:
$foo=""; isset($foo) returned false..
but under php4 it returns true, because the variable is defined.

I discovered this when converting other people's code to php4. They used isset() unnecessarily - with the implicit expectation it just tested the value of the variable as being non-null. Although I don't agree with sloppy coding, it can give an unexpected shock when run under php4 and so any uses of isset() should be searched for before you attempt to migrate.