PHP considers null is equal to zero

前端 未结 11 1995
眼角桃花
眼角桃花 2020-12-05 12:50

In php, ($myvariable==0) When $myvariable is zero, the value of the expression is true; when $myvariable is null, the value of this expression is also true. How

相关标签:
11条回答
  • 2020-12-05 13:04

    There's an is_null function, but this will just replace your $myvariable!=null

    0 讨论(0)
  • 2020-12-05 13:06
    $myvariable === 0
    

    read more about comparison operators.

    0 讨论(0)
  • 2020-12-05 13:06

    For my case i found this soulution and it works for me :

    if ($myvariable === NULL) {
        codehere...
    }
    
    0 讨论(0)
  • 2020-12-05 13:08

    Try ($myvariable === 0) which will not perform type coercion.

    0 讨论(0)
  • 2020-12-05 13:08

    If your zero could be a string, you should also considere checking the "zero string"

    ($myvariable === 0 || $myvariable === '0')
    
    0 讨论(0)
  • 2020-12-05 13:12

    To identify as null or zero by:

    • is_int($var) if a variable is a number or a numeric string. To identify Zero, use is_numeric($var) is also the solution or use $var === 0

    • is_null($var) if a variable is NULL

    0 讨论(0)
提交回复
热议问题