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.
Related
This question already has answers here:
Why does PHP consider 0 to be equal to a string?
(9 answers)
Closed 6 years ago.
I've got some weird actions from my php script and finally figured out that this following statement makes problems. Because 0 is equal some string in php.
if(0 == "whats Up?")
{
echo 42;
}
With triple "=" it do what I expected. It is possible for you to give me a briefly answer what is the reason and idea behind this behavior of php? Why did they implement php like this?
I mean I know that 1 == "1" is true and 1 === "1" is not. This is also in python. I also learn from somewhere that 0 could be understandable as false but this example above has no explication for me. But I am sure that you know it.
Thank you in advance
That's because of Type Juggling. The second operand gets converted to an integer and 0 == 0 is true.
var_dump((int) "whats Up?"); // int(0)
This question already has answers here:
Why does PHP consider 0 to be equal to a string?
(9 answers)
Closed 8 years ago.
When I run the follow code in PHP
if('yes' == 0)
echo 'a';
else
echo 'b';
The output is a.
I don't understand what happen?
And can I convert the php code to C source code to have a look what real happening?
PHP is a dynamically typed language, and == is a loose comparison operator, meaning it will first cast values it compares to one type, int for that matter, and then compare them; strings are being cast to integers by taking numericals from the left part, so 1abc casts to 1. By that logic yes cast to 0, and 0 == 0 yields true.
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.
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
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