函数 TRUE/FALSE 返回值

大量的内部函数被重写,使之在成功执行时返回 TRUE,而在失败时返回 FALSE。这就如同 PHP/FI 2.0 分别返回 0 和 -1 一样。这种改变使得更多的逻辑代码得以执行,比如 $fp = fopen("/your/file") or fail("darn!");。因为 PHP/FI 2.0 对函数执行失败的返回值没有明确的规定,所以在使用 2.0 到 3.0 转换器时必须手工检查函数返回值带来的影响。

例子 D-9. 从 2.0 移植:返回值,旧有代码

$fp = fopen($file, "r");
if ($fp == -1);
    echo("无法读取文件:$file。<br />\n");
endif;

例子 D-10. 从 2.0 移植:返回值,新的代码

$fp = @fopen($file, "r") or print("无法读取文件:$file。<br />\n");


add a note add a note User Contributed Notes
oliver at hankeln-online dot de
07-Aug-2002 11:22
I guess you are indeed wrong. You are right, 'FALSE' is beeing evaluated as a string. But it is compared with a number, so automatic type conversion takes place. 'FALSE' as well as 'FOO' and so on are converted to 0. And (0==0) is obviously true. So the number does matter. Try an other number...

Oliver
wildpeaks at warpdawg dot com
07-Aug-2002 10:30
'FALSE' is interpreted as a string containing the word false and not as a boolean value, and comparing a non-null string to a number, whatever the number is, returns true (i may be wrong).
dtampe at groupadress dot com
26-Oct-2001 01:27
Try this:

if(0=='FALSE')
     echo "It works but it should not work";

1 days of headheache...

Hope it helps!
DamTam