Double equal and triplex equal in php

I searched on StackOverflow and Google and I cannot find the answer to this question:

Should we always use triple equality in PHP for validation?

For example, I have a variable:

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

Now, I want to say whether it is necessary to check numeric fields, for example:

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

Since === also checks the type, would it be better if we always use === ?

+7
source share
7 answers

It all depends on the script you are writing, there is not a single correct answer for this. Having said that, there are not many situations where you do not yet know the type of a variable (except, possibly, user input).

It is for this reason that I stick to using == and use only === when there can be more than one type of variable.

== excellent in most cases, it would not have been invented if you had not used it :)

+5
source

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

== useless.

โ€ฃ Not transitive. "foo" == TRUE and "foo" == 0 ... but of course TRUE! = 0.

โ€ฃ == is converted to numbers when possible, which means that it is converted to floats when possible. Such large hexadecimal strings (such as password hashes) can sometimes be compared to true when not. Even JavaScript does not.

โ€ฃ For the same reason, "6" == "6", "4.2" == "4.20" and "133" == "0133". But keep in mind that 133! = 0133, because 0133 is octal.

โ€ฃ === compares values โ€‹โ€‹and type ... except for objects, where === is true only if both operands are actually the same object! For objects, == compares both the value (of each attribute) and the type, which is what === is for any 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

At the same time, when you are absolutely sure that the type is not a problem when creating simple expressions, == works quite well in my experience. Just be vigilant.

+6
source

It depends on what you want to do.
Considering that the data comes from forms as strings, == convenient because it can compare, for example, strings that represent numbers with numbers without any additional casting.

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

No, itโ€™s not always better to use === .

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

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


You can use comparison === :

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

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

or

If you must interpret the value as int

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

or

If you do not need the actual type:

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

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

And yes, itโ€™s even, although itโ€™s strange. It is best to write this if(is_numeric($x) && $x == 1)

0
source

if you expect the variable you pass to will (and should) be integer than you should use a triple equal, if not, then you should avoid this.

Although, if you really want to use === , than you should convert the variables to the type you want along the way, as in this example:

 if ((int) $var === 1) { // will return true if the $var is 1 } 
0
source

If you need such a strong check, then the answer will be: yes, definitely use === .

== also does some very strange things, such as comparing a completely different line as equal, just because they are numerically equivalent. Thus, === is likely to be the best tool for you in most situations.

-one
source

All Articles