What does !== comparison operator in PHP mean? - php

I saw
if($output !== false){
}
It's an exclamation mark with two equals signs.
It almost works like not equal. Does it has any extra significance?

They are the strict equality operators ( ===, !==) , the two operands must have the same type and value in order the result to be true.
For example:
var_dump(0 == "0"); // true
var_dump("1" == "01"); // true
var_dump("1" == true); // true
var_dump(0 === "0"); // false
var_dump("1" === "01"); // false
var_dump("1" === true); // false
More information:
PHP Comparison Operators

PHP’s === Operator enables you to compare or test variables for both equality and type.
So !== is (not ===)

!== checks the type of the variable as well as the value. So for example,
$a = 1;
$b = '1';
if ($a != $b) echo 'hello';
if ($a !== $b) echo 'world';
will output just 'world', as $a is an integer and $b is a string.
You should check out the manual page on PHP operators, it's got some good explanations.

See this question: How do the equality (==) and identity (===) comparison operators differ?.
'!==' is the strict version of not equal. I.e. it will also check type.

yes, it also checks that the two values are the same type. If $output is 0, then !== will return false, because they are not both numbers or booleans.

Related

type casting and equal operator precedence in php

Why do these 2 statements not output the same result?
The only reason I can imagine is operator precedence which appears to the same for == and ===.
$a = (bool) 4 == 4;
$b = (bool) 4 === 4;
var_dump($a); // bool(true)
var_dump($b); // bool(false)
Yes, operator precedence is the same for == and ===. Clearly the difference here is the operator itself.
First we have to acknowledge that type casting has a higher precedence than these two comparison operators. So, in reality, you're doing:
$a = (TRUE == 4);
$b = (TRUE === 4);
When you do a == you're simply trying to see if the values are equal. Only values of the same type can be compared. Since you start with a boolean, the number 4 will therefore also be turned into a boolean. This is called type juggling. We already know that (bool)4 is TRUE. So $a must be TRUE.
However, when you do a === there is no type juggling, instead it will only return TRUE if the two operands have the same value and type. Since a boolean isn't the same type as an integer $b must be FALSE.

Why PHP condition getting true even if i am checking equal to

<?php
$test = "-3,-13";
if ($test == -3) {
echo "yay";
} else {
echo "nay";
}
?>
why it always runs through if condition and not going in else condition? I am new to php so do not know what's going on here.
The string is converted into an integer "-3, 46, blala" -> -3 , then the condition is evaluated.
Use the === operator to avoid conversion.
Most of the time, you do not want to let php do the conversion in your place (security problem). Rather, the request is refused.
As PHP documentation says
Example Name Result
$a == $b Equal TRUE if $a is equal to $b after type juggling.
$a === $b Identical TRUE if $a is equal to $b, and they are of the same type.
When we compare a number with a string, the string is converted to a number and the comparison performed numerically.
var_dump(0 == "a"); // 0 == 0 -> true
var_dump("1" == "01"); // 1 == 1 -> true
https://www.php.net/manual/en/language.operators.comparison.php

Negation (!) does not change the outcome of preg_match in PHP [duplicate]

In PHP, is
if(!$foo)
equivalent with
if($foo != true)
or with
if($foo !== true)
or is it even something completly different of both?
Note that,
== OR != compares the values of variables for equality, type casting as necessary. === OR !== checks if the two variables are of the same type AND have the same value.
This answer will give you better explanation of this concept:
https://stackoverflow.com/a/80649/3067928
if(!$foo)
is the equivalent to
if($foo != true)
so
$foo = null;
if(!$foo){
echo "asd";
}
will ouptut "asd"
Its not the same
!= is No equal (Returns true if is not equal)
!== is Not identical (Returns true if is not equal , or they are not of the same type)
$a != $b
TRUE if $a is not equal to $b after type juggling.
$a !== $b
TRUE if $a is not equal to $b, or they are not of the same type.
See type juggling in PHP for more info on type juggling.
Sources : php.net

Using PHP identical comparision operators with primitive types does make any sense?

Can't get the point of === and !== with primitive types:
$a === $b TRUE if $a is equal to $b, and they are of the same type.
$a !== $b TRUE if $a is not equal to $b, or they are not of the same type.
The assuming that $request->getMethod() returns GET or POST (as string) and that $form->isValid() returns a boolean true or false, the following code:
if('POST' === $request->getMethod() || (false === $form->isValid())) :
endif;
Does make any sense in respect of this shorter one:
if('POST' == $request->getMethod() || !$form->isValid()) :
endif;
You have truty and falsy values in PHP. For instance, 0, '', array() are falsy values. If you use == it will match those values with the truty/falsy values:
var_dump(true == 'hello'); // true because a not empty string is a truty value
var_dump(false == 0); // true
=== will match not only the value but the type also:
var_dump(true === 'hello'); // false, true is a boolean and 'hello' a string
var_dump(false === 0); // false, false is a boolean and 0 is a string
This will become a problem when a function can return 0 or false, strpos for example.
There are also other factors with the ==. It will type cast values to a int if you compare 2 different types:
var_dump("123abc" == 123); // true, because '123abc' becomes `123`
This will be problematic if you compare a password: http://phpsadness.com/sad/47
They are sometimes necessary. For example when using strpos to check if a string is contained in another string you have to distinguish 0 from false.
wrong:
if(strpos($haystack,$needle))...
right:
if(strpos($haystack,$needle) !== false)...
== will sometimes have odd behavior when comparing different types. E.g. 'POST' would be considered equal to 0. That's why many people usually use ===, it avoids type-juggling problems.
In your case it shouldn't make a difference though.
although it may not be needed,
(false===$form->isValid())
and
!$form->isValid()
are not the same as the first is checking to see if the value of $form->isValid() is false, while the second is checking if $form->isValid() is a falsey value, so for example if $form->isValid() returns null then the first statement will not evaluate to true while the second one will evluate to true.

What does === do in PHP

I have been programming in PHP for a while but I still dont understand the difference between == and ===. I know that = is assignment. And == is equals to. So what is the purpose of ===?
It compares both value and type equality.
if("45" === 45) //false
if(45 === 45) //true
if(0 === false)//false
It has an analog: !== which compares type and value inequality
if("45" !== 45) //true
if(45 !== 45) //false
if(0 !== false)//true
It's especially useful for functions like strpos - which can return 0 validly.
strpos("hello world", "hello") //0 is the position of "hello"
//now you try and test if "hello" is in the string...
if(strpos("hello world", "hello"))
//evaluates to false, even though hello is in the string
if(strpos("hello world", "hello") !== false)
//correctly evaluates to true: 0 is not value- and type-equal to false
Here's a good wikipedia table listing other languages that have an analogy to triple-equals.
It is true that === compares both value and type, but there is one case which hasn't been mentioned yet and that is when you compare objects with == and ===.
Given the following code:
class TestClass {
public $value;
public function __construct($value) {
$this->value = $value;
}
}
$a = new TestClass("a");
$b = new TestClass("a");
var_dump($a == $b); // true
var_dump($a === $b); // false
In case of objects === compares reference, not type and value (as $a and $b are of both equal type and value).
The PHP manual has a couple of very nice tables ("Loose comparisons with ==" and "Strict comparisons with ===") that show what result == and === will give when comparing various variable types.
It will check if the datatype is the same as well as the value
if ("21" == 21) // true
if ("21" === 21) // false
=== compares value and type.
== doesn't compare types, === does.
0 == false
evaluates to true but
0 === false
does not
Minimally, === is faster than == because theres no automagic casting/coersion going on, but its so minimal its hardly worth mentioning. (of course, I just mentioned it...)
It's a true equality comparison.
"" == False for instance is true.
"" === False is false

Categories