This question already has answers here:
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
(13 answers)
Closed 1 year ago.
I know that this is caused by type conversion, but can someone explain me, what exactly is happening here, when:
(2 == '2abd') // true
(3 == '3a') // true
I was a bit surprised when running into a bug by comparing a string variable against strings and integers.
Thank you very much!
If we convert 2abd and 3a to integers, you'll get 2 and 3 accordingly.
Since 2 == 2 and 3 == 3 both statements return true.
Online test case:
<?php
var_dump(2 == '2abd'); // true
var_dump(3 == '3a'); // true
var_dump((int) '2abd'); // 2
var_dump((int) '3a'); // 3
var_dump(2 == 2); // true
var_dump(3 == 3); // true
For more info about operator precedence, please take a look at:
PHP's help page regarding this
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
Rules about converting to integers
Related
This question already has answers here:
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
(13 answers)
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 6 years ago.
I've read somewhere that === is more secure than ==
So should I use something like this:
if ($user === "") {
echo "you are not logged in";
}
instead of
if ($user == "") {
echo "you are not logged in";
}
? The question is probably stupid, but I never really used === so I don't know much about it from security standpoint.
I am strictly interested in why === is more secure than ==
The duplicate doesn't address that.
This question already has answers here:
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
(13 answers)
Closed 6 years ago.
I've been trying to make a system where certain flags in a MySQL database are checked with php. To see if a certain letter is in the string, I use the function strpos(). If the letter I'm checking for happens to be the first one in the string the function returns 0. However this result seems to be synonymous with the Boolean value for false. I tested this theory with the below code:
if(0 == false){
echo 'true';
}
low and behold this evaluates to true and echoes the output inside the block. So, is there a logical way to distinguish between a character being at index 0 or not being part of the string at all? One option that occurred to me was to basically prepend the flags string with some character like an underscore. This way no meaningful characters would ever be at index 0 of the function's return value. That seems like a very dirty patchy way to fix things though. If you have any other ideas I would greatly appreciate your help.
Thanks for reading.
Use ===:
if(0 === false){
// will never be true
echo 'true';
}
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'm a little bit confused about truth in PHP.
My understanding is that 0 evaluates to false, and non-empty strings (unless the string is "0") evaluate to true.
This is as I expect:
var_dump((bool) 0); // prints "boolean false"
var_dump((bool) 'someString'); // prints "boolean true"
But then I am surprised by the following result:
var_dump((0=='someString')); // prints "boolean true"
My question is, why does 0=='someString' evaluate to true?
When using the comparison (==) operator strings will be converted to an integer when compared to another integer. This is because of type juggling in PHP. So "someString" evaluates to zero because it is converted to an integer and has no leading digits. If you use the the identical operator (===) type conversions are not done so "someString" is treated a literal string and your statement will then evaluate to false.
The following will evaluate to false when type juggling is performed. Everything else will be evaluated as true:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)
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 :)
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