如何在PHP中检测if(float)0 == 0或null

mrf*_*lka 8 php decimal detect zero

如果变量值为0(浮点数),它将通过所有这些测试:

    $test = round(0, 2); //$test=(float)0

    if($test == null)
        echo "var is null";
    if($test == 0)
        echo "var is 0";
    if($test == false)
        echo "var is false";
    if($test==false && $test == 0 && $test==null)
        echo "var is mixture";
Run Code Online (Sandbox Code Playgroud)

我假设它只会在($ test == 0)时通过

我找到的唯一解决方案是使用函数is_number()检测$ test是否为数字,但是我可以检测浮点变量是否等于零吗?

Mar*_*der 19

===还使用检查数据类型:

$test = round(0, 2); // float(0.00)

if($test === null) // false
if($test === 0) // false
if($test === 0.0) // true
if($test === false) // false
Run Code Online (Sandbox Code Playgroud)

  • `0 == 0.0 && 0!== 0.0`浮点数和整数与三等于不可比. (2认同)