7

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 example, I have a variable:

$x = '1';

if($x == 1)  // will work
if($x === 1) // will not

Now, my point is if we need to validate numeric fields like:

if(is_numeric($x) && $x == '1') { will be the equivalent to if($x === 1)

Since === also validate the type, will it be better if we always use the ===?

Ken
  • 1,091
  • 2
  • 8
  • 16

7 Answers7

6

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.

Alex
  • 631
  • 1
  • 8
  • 33
5

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 :)

Drahcir
  • 11,772
  • 24
  • 86
  • 128
  • 1
    “It depends entirely on the script you're writing,” +1 – TimWolla Dec 17 '11 at 20:08
  • Be aware that `"1e3" == "1000"` and `"61529519452809720693702583126814" == "61529519452809720000000000000000"` both are `true` so you shouldn't use `==` for comparing strings. – ausi Jun 28 '14 at 14:08
  • 1
    "it wouldn't have been invented if you weren't supposed to use it" - sorry don't buy that. :) especially given how php initially evolved. – bravmi Sep 24 '14 at 05:13
  • @ausi, just tried the 2nd string comparison, and it evaluates to false for me. I'm on 5.5.21 on my machine. Why would those evaluate to be the same – Swaraj Mar 02 '15 at 23:01
  • 1
    @Swaraj The second comparison was fixed in PHP 5.4.4 [Bug #54547](https://bugs.php.net/bug.php?id=54547). Why they did evaluate to true before 5.4.4 is described here: http://phpsadness.com/sad/47 – ausi Mar 03 '15 at 08:30
3

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 ===.

TimWolla
  • 31,849
  • 8
  • 63
  • 96
clyfe
  • 23,695
  • 8
  • 85
  • 109
1
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') { ...
Charles Sprayberry
  • 7,741
  • 3
  • 41
  • 50
0

I would say it is better to always use === and remove one = in cases you can justify.

And yes it's equal, though weird. Better way to write it is if(is_numeric($x) && $x == 1)

Fox
  • 2,348
  • 19
  • 19
0

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
}
vertazzar
  • 1,053
  • 7
  • 10
-1

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.

Ken Wayne VanderLinde
  • 18,915
  • 3
  • 47
  • 72