我必须将我的状态更改为方法中的单词,但是当我将参数作为字符串传递给方法时,我从以下两个条件得到结果:
public static function Status($status)
{
if ($status == true || $status == True || $status == 'true' || $status == 'True' || $status == 1)
{
echo "true";
}
if ($status == false || $status == False || $status == 'false' || $status == 'False' || $status == 0)
{
echo "false";
}
}
Run Code Online (Sandbox Code Playgroud)
当我将'False'值作为字符串传递给方法时,我得到truefalse结果,但我对字符串值没有任何问题.
您应该使用===而不是==检查值是否相同.因为任何字符串都将被视为true比较而不检查变量类型.所以,将代码更改为
function Status($status)
{
if ($status === true || $status === True || $status === 'true' || $status === 'True' || $status === 1)
{
echo "true";
}
if ($status === false || $status === False || $status === 'false' || $status === 'False' || $status === 0)
{
echo "false";
}
}
Run Code Online (Sandbox Code Playgroud)
http://php.net/manual/en/language.operators.comparison.php