标签:php
我必须将我的状态更改为方法中的单词,但是当我将参数作为字符串传递给方法时,我从以下两个条件得到结果:
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";
}
}
当我将’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";
}
}
标签:php
来源: https://codeday.me/bug/20190528/1170570.html