Double equals and tripple equals in php

后端 未结 7 466
囚心锁ツ
囚心锁ツ 2021-01-18 01:54

I searched on StackOverflow and Google and I can\'t find the answer to this question:

Should we always use the triple equal in PHP for validation?

For exampl

7条回答
  •  醉梦人生
    2021-01-18 02:36

    if (is_numeric($x) && $x == '1') { ...
    

    This looks redundant to me. Why do we need to check if $x is_numeric AND the value '1'? We know '1' is numeric so if it is equal to '1' then it must be a number.


    You could use === comparison:

    If you're fine with interpreting it as a string:

    if ($x === '1') { ...
    

    or

    If you must interpret the value as an int

    if ((int) $x === 1) { ...
    

    or

    If you don't care about the actual type:

    if ($x == '1') { ...
    

提交回复
热议问题