Three equals signs in php comparison [duplicate] - php

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 :)

Related

Type conversion string / integer [duplicate]

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

PHP 0 vs False ambiguity [duplicate]

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';
}

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

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

Is there any difference between "and" and "&&" operators in PHP? [duplicate]

This question already has answers here:
'AND' vs '&&' as operator
(10 answers)
Closed 9 years ago.
I have the following code and don't think and is needed, i.e. && should be used, as there is nothing to assign the left part to?
if($_REQUEST['foo'] != 'abc' and $_REQUEST['bar'] == 'def')
{
echo "all your base";
}
So it should be:
if($_REQUEST['foo'] != 'abc' && $_REQUEST['bar'] == 'def')
{
echo "all your base";
}
In your case, && and and do the same thing, but take a look at the operator precedence. You can see that && and and are not on the same level, so mixing that could give you unexpected results in some cases - I recommend always using &&, but that's your choice.
&& and "and" are two different things, because of their precedence.
The reason for the two different variations of "and" and "or" operators is that they operate at different precedences. (See Operator Precedence.)
See Logical Operators
And Operator Precedence
'and' and '&&' is the same operator, apart from precedence differences.
They are different operators, but they operate the same, apart from precedence differences.

Categories