php可以在浏览器中直接回显boolean true吗?
问题描述:
<?php
function show(){
$num=rand(0,1);
if ($num===1){
return true;
}
else
return false;
}
echo (boolean)show();
?>
即使我将它转换为布尔值,它只显示为1时为真。
但是我读了一本PHP书,告诉它有效。这让我感到困惑。
感谢您的帮助。
even though I cast it to boolean it only show 1 when true.
But I read a php book which tell it works. This let me confused.
Thank you for your help.
答
num = rand( 0 , 1 );
if (
num=rand(0,1); if (
num === 1){
返回 true;
}
else
return false;
}
echo(boolean)show();
?>
num===1){ return true; } else return false; } echo (boolean)show(); ?>
即使我将它转换为布尔值,它只显示为1时为真。
但是我读了一本PHP书,告诉它有效。这让我感到困惑。
感谢您的帮助。
even though I cast it to boolean it only show 1 when true.
But I read a php book which tell it works. This let me confused.
Thank you for your help.
如果您的目的仅仅是回应结果,为什么不简单地返回'true'和'false '作为字符串?
如果必须返回值(0或1),则将您的ECHO更改为以下内容:
ECHO((布尔)show()== 1)?'true':'false';
编程是保持简单是最好的。
If your purpose is merely to echo the result, why not simply return 'true' and 'false' as strings ?
If you must return the value (which is 0 or 1), then change your ECHO to the following:
ECHO ((boolean)show()==1)?'true':'false';
Programming is at is best when you keep it simple.