What does === mean in php [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What does “===” mean?
i am seeing === often in php statements, but don't know what it mean.
e.g if ($pwd === PwdHash($pass,substr($pwd,0,9))).
thanks

It tests equality, but unlike == it requires that the two operands be of the same type as well as value.
For instance, "1" == 1 will be true, but "1" === 1 is false because the type is different.

php has two types of equal comparison operator == and ===
== check for the equalization but not strict mean it will return true for ('123'==123)
=== is a strict equal operator it will return false for the ('123'===123)
read more about these from here

Related

Trouble using Yoda conditions on PHP IF statement [duplicate]

This question already has answers here:
Does variables' order matter when doing == and === comparisons? [duplicate]
(3 answers)
Closed 3 years ago.
Original question
i read sometime ago in an article i can't find that when doing a comparison the order matters for some reason.
i couldn't find any information on the subject of the order of comparing in PHP.
it obviously won't make a dramatic change but i am curious to know if there is any merit to this.
throughout the project i am working on the comparison is done as $x === true.
is there any difference in doing the comparison in the opposite order, as in true === $x?
Conclusion
Apparently what i referred to in the original question is a programming style called "Yoda conditions".
This wiki page gives a good explanation about this style.
This answer made me understand the concept, just note there is a small mistake there.
Here's my take on it:
The main reason to use this style is to avoid an accidental assignment with = when you meant to compare with ==.
if you want to check if a variable loosely has the same value as what you compare it to, use:
if(false == $var) // evaluates to true if $var is equal to false
over
if($var == false) // evaluates to true if false is equal to $var
to prevent
if($var = false) // assigns false to $var and evaluates to false
while
if(false = $var) // is a syntax error
if you want to check if a variable strictly has the same value as what you compare it to, use:
if(false === $var) // evaluates to true if $var is identical to false
over
if($var === false) // evaluates to true if false is identical to $var
it doesn't matter in the context of using Yoda style or not as this isn't really an issue with strict comparison because it's pretty hard to confuse = with === but if you use it for == then also use it for === to be consistent.
All in all, I think the merit of Yoda notation lies in the emphasized distinction it makes between comparison and assignment in a condition.
Those are called "Yoda Comparisons". For === and == it would not matter, but consider this:
if($a = 0)
In this case $a is being set to zero, not compared. In addition, the comparison you think you're making will not evaluate at all, but the comparison operation will proceed as if there was a comparison made. It will always fall through to false Flip it:
if(0 = $a)
Now an error would be thrown.

PHP - order of values in conditions AKA Yoda notation [duplicate]

This question already has answers here:
Does variables' order matter when doing == and === comparisons? [duplicate]
(3 answers)
Closed 3 years ago.
Original question
i read sometime ago in an article i can't find that when doing a comparison the order matters for some reason.
i couldn't find any information on the subject of the order of comparing in PHP.
it obviously won't make a dramatic change but i am curious to know if there is any merit to this.
throughout the project i am working on the comparison is done as $x === true.
is there any difference in doing the comparison in the opposite order, as in true === $x?
Conclusion
Apparently what i referred to in the original question is a programming style called "Yoda conditions".
This wiki page gives a good explanation about this style.
This answer made me understand the concept, just note there is a small mistake there.
Here's my take on it:
The main reason to use this style is to avoid an accidental assignment with = when you meant to compare with ==.
if you want to check if a variable loosely has the same value as what you compare it to, use:
if(false == $var) // evaluates to true if $var is equal to false
over
if($var == false) // evaluates to true if false is equal to $var
to prevent
if($var = false) // assigns false to $var and evaluates to false
while
if(false = $var) // is a syntax error
if you want to check if a variable strictly has the same value as what you compare it to, use:
if(false === $var) // evaluates to true if $var is identical to false
over
if($var === false) // evaluates to true if false is identical to $var
it doesn't matter in the context of using Yoda style or not as this isn't really an issue with strict comparison because it's pretty hard to confuse = with === but if you use it for == then also use it for === to be consistent.
All in all, I think the merit of Yoda notation lies in the emphasized distinction it makes between comparison and assignment in a condition.
Those are called "Yoda Comparisons". For === and == it would not matter, but consider this:
if($a = 0)
In this case $a is being set to zero, not compared. In addition, the comparison you think you're making will not evaluate at all, but the comparison operation will proceed as if there was a comparison made. It will always fall through to false Flip it:
if(0 = $a)
Now an error would be thrown.

Three equals signs in php comparison [duplicate]

This question already has answers here:
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
(13 answers)
Closed 9 years ago.
I was working on some code made by someone else and came across in if statement that looks like this
IF($res === true){
do_something();}
This code is called many times and seems to be working. What is it doing?
I know one equals is assignment and two is comparison. What does the three equals sign operator do?
The easiest way to see it, is that == checks equality and === checks identicality. Equality will check the value, but identicality will check the variable type too.
Examples:
var_dump('true' == true); // bool(true)
var_dump('true' === true); // bool(false)
This is the "identical" operator. They'll need to be exactly the same to pass the condition.
The 3 different equals
Three === means that result has to be identical. Worded it wrong first time :)

what is the difference between == and ===? [duplicate]

This question already has answers here:
What does === do in PHP
(8 answers)
Closed 8 years ago.
I'm learning about cakephp, and I see much conditionals like:
if( x === y){
}
I've looked for it, but I don't find anything.
== compares the values of two variables. If they are of different types, they are converted to a common type and then compared.
===, on the other hand, is more strict. It requires the two sides to be of the same type as well.
php> = 5 == "5"
true
php> = 5 === "5"
false
== wil do auto type conversion, === won't
This means that:
0 == "0" evaluates to TRUE, because internally when comparing strings and numbers, a string is converted to a number when using ==.
0 === "0" evaluates to FALSE, there is no type conversion done and a integer 0 is not equal to a string.
More info in the documentation and more documentation.

what is the difference between '==' and '==='? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Javascript === vs == : Does it matter which “equal” operator I use?
Not really relative to any type of code, just in general?
usually === also represents equality of type.
so:
1 == "1" //true
1 === "1" //false
=== is the identical operator; it returns true when both the value and the type of the two operands are the same. == is the equal operator; it does not check types, just values.
Read more here.
=== compares the value as well as type of variable
== doesn't compare type

Categories