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

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

Related

== operator for string [duplicate]

This question already has answers here:
Understanding PHP type coercion
(4 answers)
Closed 7 years ago.
Why does this code echo Yes. even though variables are not equal!
$a = '0e462097431906509019562988736854';
$b = '0e830400451993494058024219903391';
if( $a == $b ) echo 'Yes.';
else echo 'No!';
Both will treated as numbers, and PHP had limitations in number storage before.
So check that.
Try to use '==='. it will check the type also, so those will not convert to numbers.
Refer this question and its answers.
You want strcmp, not the equality operator.
try it, with using strcmp function:
if(int strcmp ($a,$b)===0) echo 'Yes.';
else echo 'No!';
Try using '===' instead of '=='.
'==' has a "weaker" comparison because it does not check for type.
'===' on the other hand, checks for the type as well, and it is generally good practice to be more explicit when you compare two things.

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.

IF Statement always executed in PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why does PHP consider 0 to be equal to a string?
php string comparasion to 0 integer returns true?
it seems that as one has in PHP an if-statement where a function some_function() returns zero
<?php
if( some_function() == "whatever_you_want" ) { ... }
the statement will always be executed since
<?php
echo some_function() == "whatever_you_want";
is then TRUE.
Why behaves PHP in such a counter intuitive way?
This is a defined behavior of PHP when you compare a number value and a string value:
If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value.
Use strict value comparison with === or !== and you’re getting the expected result.

What does === mean in php [duplicate]

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

Categories