What does === do in PHP - 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

Related

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

False converted to null automatically in my PHP

I'm new to PHP and I found in my code, when I pass a FALSE to a function. It converted to null immediately. I've read some articles knowing that False and null are equal. But I don't know when this conversion happens.
Below is my code
function equal($expected, $actual){
if($expected == $actual) { //... }
}
function foo(){
$signal = getSignal();
equal(FALSE, $signal->good); //...
}
You will need to use triple equal signs === for equality. For example
false == null; // true
false === null; // false
0 == false; // true
0 === false; // false
Check the docs on comparison operators
No, they are NOT converted. You can var_dump($variable) at any time to see both the type and the value of $variable any time. But there are a few things which evaluates to false with the == comparison operator. So in fact false == null will evaluate to true just as false == 0 or false == "0" in PHP. This is why the comparison operator === comes into the picture - using that instead of == in above example, all will evaluate to false instead of true.
For more information, see http://php.net/manual/en/language.operators.comparison.php

Not equal to != and !== in PHP

I've always done this: if ($foo !== $bar)
But I realized that if ($foo != $bar) is correct too.
Double = still works and has always worked for me, but whenever I search PHP operators I find no information on double =, so I assume I've always have done this wrong, but it works anyway. Should I change all my !== to != just for the sake of it?
== and != do not take into account the data type of the variables you compare. So these would all return true:
'0' == 0
false == 0
NULL == false
=== and !== do take into account the data type. That means comparing a string to a boolean will never be true because they're of different types for example. These will all return false:
'0' === 0
false === 0
NULL === false
You should compare data types for functions that return values that could possibly be of ambiguous truthy/falsy value. A well-known example is strpos():
// This returns 0 because F exists as the first character, but as my above example,
// 0 could mean false, so using == or != would return an incorrect result
var_dump(strpos('Foo', 'F') != false); // bool(false)
var_dump(strpos('Foo', 'F') !== false); // bool(true), it exists so false isn't returned
!== should match the value and data type
!= just match the value ignoring the data type
$num = '1';
$num2 = 1;
$num == $num2; // returns true
$num === $num2; // returns false because $num is a string and $num2 is an integer
$a !== $b TRUE if $a is not equal to $b, or they are not of the same type
Please Refer to http://php.net/manual/en/language.operators.comparison.php
You can find the info here: http://www.php.net/manual/en/language.operators.comparison.php
It's scarce because it wasn't added until PHP4. What you have is fine though, if you know there may be a type difference then it's a much better comparison, since it's testing value and type in the comparison, not just value.

What does !== comparison operator in PHP mean?

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.

Categories