Double equals and tripple equals in php

后端 未结 7 464
囚心锁ツ
囚心锁ツ 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:28

    If you want such a strong validation, then the answer is: yes, definitely use ===.

    The == also does some very weird things, like comparing completely different string as equal, just because they are numerically equivalent. So === will probably be a better tool for you in most situations.

    0 讨论(0)
  • 2021-01-18 02:32

    It depends on what you want to do.
    Given that from forms, data comes as strings, == is handy because it can compare, for example, strings that represent numbers with numbers with no additional type casting.

    if ($_GET['amount'] == 10) {
        //...
    }
    

    So no, it's not better to always use ===.

    0 讨论(0)
  • 2021-01-18 02:33

    From http://me.veekun.com/blog/2012/04/09/php-a-fractal-of-bad-design/

    == is useless.

    ‣ It’s not transitive. "foo" == TRUE, and "foo" == 0… but, of course, TRUE != 0.

    ‣ == converts to numbers when possible, which means it converts to floats when possible. So large hex strings (like, say, password hashes) may occasionally compare true when they’re not. Even JavaScript doesn’t do this.

    ‣ For the same reason, "6" == " 6", "4.2" == "4.20", and "133" == "0133". But note that 133 != 0133, because 0133 is octal.

    ‣ === compares values and type… except with objects, where === is only true if both operands are actually the same object! For objects, == compares both value (of every attribute) and type, which is what === does for every other type. What.

    See http://habnab.it/php-table.html

    And http://phpsadness.com/sad/47

    And http://developers.slashdot.org/comments.pl?sid=204433&cid=16703529

    That being said, when you are absolutely sure type is not an issue when you are creating simple expressions, == works well enough in my experience. Just be vigilant.

    0 讨论(0)
  • 2021-01-18 02:33

    if you're expecting that variable that you're passing will (and must) be integer than you should use triple equal, if not than you should avoid that.

    Although, if you really want to use === than you should be doing conversion of variables to the type that you want along the way, like on this example:

    if ((int) $var === 1)
    {
    
        // will return true if the $var is 1
    }
    
    0 讨论(0)
  • 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') { ...
    
    0 讨论(0)
  • 2021-01-18 02:38

    It depends entirely on the script you're writing, there's not one correct answer for this. Having said that, there aren't many situations where you don't already know the type of the variable (except perhaps user input).

    This is the reason I stick to using == and only use === when there could be more than one type of the variable.

    The == is fine most of the time, it wouldn't have been invented if you weren't supposed to use it :)

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