How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
What is the difference between ==
and ===
?
- How exactly does the loosely
==
comparison work? - How exactly does the strict
===
comparison work?
What would be some useful examples?
php comparison operators equality identity-operator
add a comment |
What is the difference between ==
and ===
?
- How exactly does the loosely
==
comparison work? - How exactly does the strict
===
comparison work?
What would be some useful examples?
php comparison operators equality identity-operator
2
Here is detailed comparison of both operator in JavaScript with performance test
– Zaheer Ahmed
Jan 8 '14 at 20:08
add a comment |
What is the difference between ==
and ===
?
- How exactly does the loosely
==
comparison work? - How exactly does the strict
===
comparison work?
What would be some useful examples?
php comparison operators equality identity-operator
What is the difference between ==
and ===
?
- How exactly does the loosely
==
comparison work? - How exactly does the strict
===
comparison work?
What would be some useful examples?
php comparison operators equality identity-operator
php comparison operators equality identity-operator
edited Apr 19 '17 at 14:02
Sebastian Zartner
13k76098
13k76098
asked Sep 17 '08 at 6:56
nickfnickf
374k173585687
374k173585687
2
Here is detailed comparison of both operator in JavaScript with performance test
– Zaheer Ahmed
Jan 8 '14 at 20:08
add a comment |
2
Here is detailed comparison of both operator in JavaScript with performance test
– Zaheer Ahmed
Jan 8 '14 at 20:08
2
2
Here is detailed comparison of both operator in JavaScript with performance test
– Zaheer Ahmed
Jan 8 '14 at 20:08
Here is detailed comparison of both operator in JavaScript with performance test
– Zaheer Ahmed
Jan 8 '14 at 20:08
add a comment |
21 Answers
21
active
oldest
votes
Difference between ==
and ===
The difference between the loosely ==
equal operator and the strict ===
identical operator is exactly explained in the manual:
Comparison Operators
┌──────────┬───────────┬───────────────────────────────────────────────────────────┐
│ 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. │
└──────────┴───────────┴───────────────────────────────────────────────────────────┘
Loosely ==
equal comparison
If you are using the ==
operator, or any other comparison operator which uses loosely comparison such as !=
, <>
or ==
, you always have to look at the context to see what, where and why something gets converted to understand what is going on.
Converting rules
- Converting to boolean
- Converting to integer
- Converting to float
- Converting to string
- Converting to array
- Converting to object
- Converting to resource
- Converting to NULL
Type comparison table
As reference and example you can see the comparison table in the manual:
Loose comparisons with
==
┌─────────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬─────────┬───────┬───────┐
│ │ TRUE │ FALSE │ 1 │ 0 │ -1 │ "1" │ "0" │ "-1" │ NULL │ array() │ "php" │ "" │
├─────────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼─────────┼───────┼───────┤
│ TRUE │ TRUE │ FALSE │ TRUE │ FALSE │ TRUE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │
│ FALSE │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ TRUE │ TRUE │ FALSE │ TRUE │
│ 1 │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ 0 │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ TRUE │ TRUE │
│ -1 │ TRUE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │
│ "1" │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ "0" │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ "-1" │ TRUE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │
│ NULL │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ TRUE │ FALSE │ TRUE │
│ array() │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ TRUE │ FALSE │ FALSE │
│ "php" │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │
│ "" │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │
└─────────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴─────────┴───────┴───────┘
Strict ===
identical comparison
If you are using the ===
operator, or any other comparison operator which uses strict comparison such as !==
or ===
, then you can always be sure that the types won't magically change, because there will be no converting going on. So with strict comparison the type and value have to be the same, not only the value.
Type comparison table
As reference and example you can see the comparison table in the manual:
Strict comparisons with
===
┌─────────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬─────────┬───────┬───────┐
│ │ TRUE │ FALSE │ 1 │ 0 │ -1 │ "1" │ "0" │ "-1" │ NULL │ array() │ "php" │ "" │
├─────────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼─────────┼───────┼───────┤
│ TRUE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ 1 │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ 0 │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ -1 │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ "1" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ "0" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ "-1" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │
│ NULL │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │
│ array() │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │
│ "php" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │
│ "" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │
└─────────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴─────────┴───────┴───────┘
60
This works much the same in Javascript, btw.
– Raithlin
Sep 17 '08 at 7:11
61
does anyone else find it strange that "000" == "0000" ?
– nickf
Sep 17 '08 at 13:08
35
What always suprises me is that false == array(), and false == 0 but array() != 0, so false == array() !=/== 0? that feels weird to me.
– Pim Jager
Aug 17 '09 at 11:50
11
@Raithlin, careful of array. triple equals givesfalse
for different arrays in javascript, buttrue
for PHP as long as their values are equal.
– Pacerier
Aug 2 '13 at 10:34
13
@Raithlin, Many many more gotchas. In JavaScript:"000" != "00"
,"000" == null
,"000" == false
,"0x0" == false
,array() == 0
,false != null
,array() != null
,false == "0x0"
,false == "000"
. In PHP, it's opposite behavior:"000" == "00"
,"000" != null
,"000" != false
,"0x0" != false
,array() != 0
,false == null
,array() == null
,false != "0x0"
,false != "000"
.
– Pacerier
Aug 7 '13 at 17:03
|
show 15 more comments
The operator == casts between two different types if they are different, while the === operator performs a 'typesafe comparison'. That means that it will only return true if both operands have the same type and the same value.
Examples:
1 === 1: true
1 == 1: true
1 === "1": false // 1 is an integer, "1" is a string
1 == "1": true // "1" gets casted to an integer, which is 1
"foo" === "foo": true // both operands are strings and have the same value
Warning: two instances of the same class with equivalent members do NOT match the ===
operator. Example:
$a = new stdClass();
$a->foo = "bar";
$b = clone $a;
var_dump($a === $b); // bool(false)
3
Nitpick: === will only return true if both operands are the same type and the values are equal =)
– gnud
Feb 26 '09 at 9:16
1
@gnud That's exactly what he's shown in the example. If it was just comparing the types it would just be called a "type comparison" wouldn't it.
– Rob Stevenson-Leggett
Feb 26 '09 at 9:34
3
After using PHP for 8 years, yesterday was the first time I got caught in a situation where I should've used ===
– uuɐɯǝʃǝs
Apr 13 '09 at 13:21
3
=== true if they are equal and have same type. == true if they are equal. != true if they aren't equal. !== true if either they aren't equal, or are equal but aren't the same type.
– Jeremy C
Oct 22 '12 at 23:55
Also, using === is slightly faster than == since it doesn't need to convert the value before checking if it's equal.
– clauziere
Mar 18 '14 at 20:04
|
show 3 more comments
A picture is worth a thousand words:
PHP Double Equals ==
equality chart:
PHP Triple Equals ===
Equality chart:
Source code to create these images:
https://github.com/sentientmachine/php_equality_charts
Guru Meditation
Those who wish to keep their sanity, read no further.
- '==' converts left and right operands to numbers when possible (123 == "123foo", but "123" != "123foo"
- A hex string in quotes is occasionally a float and will be casted to it against your will.
- == is not transitive because ("0" is == to 0, and 0 is == to "" but "0" != "")
- "6" == " 6", "4.2" == "4.20", and "133" == "0133". But 133 != 0133, because 0133 is octal. But "0x10" == "16" and "1e3" == "1000"
PHP Variables that have not been declared yet are false.
False is equal to 0, blankstring and empty array and "0".
- When numbers are big enough they are == Infinity.
NAN does not == itself, but it is True.
A fresh class is == to 1.
- False is the most dangerous value because False is == to most of the other variables, mostly defeating it's purpose.
Hope:
If you are using PHP, Thou shalt not use the double equals operator, always use triple equals.
Is it really a good idea (also secure) to always use triple equals?
– Chazy Chaz
Jan 16 '17 at 22:42
2
Yes, the transitive property of triple equals makes it more secure and webscale.
– Eric Leschinski
Jan 16 '17 at 23:55
add a comment |
In regards to JavaScript:
The === operator works the same as the == operator, but it requires that its operands have not only the same value, but also the same data type.
For example, the sample below will display 'x and y are equal', but not 'x and y are identical'.
var x = 4;
var y = '4';
if (x == y) {
alert('x and y are equal');
}
if (x === y) {
alert('x and y are identical');
}
Upvoted, as this seems to be exactly the same situation for php.
– David Thomas
Aug 17 '09 at 13:40
1
@DavidThomas It's not exactly the same.See stackoverflow.com/questions/12598407/…
– xdazz
May 4 '13 at 12:45
add a comment |
An addition to the other answers concerning object comparison:
== compares objects using the name of the object and their values. If two objects are of the same type and have the same member values, $a == $b
yields true.
=== compares the internal object id of the objects. Even if the members are equal, $a !== $b
if they are not exactly the same object.
class TestClassA {
public $a;
}
class TestClassB {
public $a;
}
$a1 = new TestClassA();
$a2 = new TestClassA();
$b = new TestClassB();
$a1->a = 10;
$a2->a = 10;
$b->a = 10;
$a1 == $a1;
$a1 == $a2; // Same members
$a1 != $b; // Different classes
$a1 === $a1;
$a1 !== $a2; // Not the same object
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:50
add a comment |
In simplest terms:
== checks if equivalent (value only)
=== checks if the same (value && type)
Equivalent vs. Same: An Analogy
1 + 1 = 2 + 0 (equivalent)
1 + 1 = 1 + 1 (same)
In PHP:
true == 1 (true - equivalent in value)
true === 1 (false - not the same in value && type)
- true is boolean
- 1 is int
"=== checks if the same (value && type)", not exactly true. Two stdClass objects has the same type of 'object' (i.e. using gettype()), but PHP says they're two different things if you use strict comparison. See this.
– MAChitgarha
Sep 12 '18 at 16:44
add a comment |
It's all about data types. Take a BOOL
(true or false) for example:
true
also equals 1
and
false
also equals 0
The ==
does not care about the data types when comparing:
So if you had a variable that is 1 (which could also be true
):
$var=1;
And then compare with the ==
:
if ($var == true)
{
echo"var is true";
}
But $var
does not actually equal true
, does it? It has the int value of 1
instead, which in turn, is equal to true.
With ===
, the data types are checked to make sure the two variables/objects/whatever are using the same type.
So if I did
if ($var === true)
{
echo "var is true";
}
that condition would not be true, as $var !== true
it only == true
(if you know what I mean).
Why would you need this?
Simple - let's take a look at one of PHP's functions: array_search()
:
The array_search()
function simply searches for a value in an array, and returns the key of the element the value was found in. If the value could not be found in the array, it returns false. But, what if you did an array_search()
on a value that was stored in the first element of the array (which would have the array key of 0
)....the array_search()
function would return 0...which is equal to false..
So if you did:
$arr = array("name");
if (array_search("name", $arr) == false)
{
// This would return 0 (the key of the element the val was found
// in), but because we're using ==, we'll think the function
// actually returned false...when it didn't.
}
So, do you see how this could be an issue now?
Most people don't use == false
when checking if a function returns false. Instead, they use the !
. But actually, this is exactly the same as using ==false
, so if you did:
$arr = array("name");
if (!array_search("name", $arr)) // This is the same as doing (array_search("name", $arr) == false)
So for things like that, you would use the ===
instead, so that the data type is checked.
add a comment |
One example is that a database attribute can be null or "":
$attributeFromArray = "";
if ($attributeFromArray == ""){} //true
if ($attributeFromArray === ""){} //true
if ($attributeFromArray == null){} //true
if ($attributeFromArray === null){} //false
$attributeFromArray = null;
if ($attributeFromArray == ""){} //true
if ($attributeFromArray === ""){} //false
if ($attributeFromArray == null){} //true
if ($attributeFromArray === null){} //true
add a comment |
Given x = 5
1) Operator : == is "equal to". x == 8
is false
2) Operator : === is "exactly equal to" (value and type) x === 5
is true, x === "5"
is false
add a comment |
Few of the examples
var_dump(5 == 5); // True
var_dump(5 == "5"); // True because == checks only same value not type
var_dump(5 === 5); // True
var_dump(5 === "5"); // False because value are same but data type are different.
P.S.
== Compares the value only, it won't bother about the data types
vs.
=== Compares the values and data types
what's the problem with this answer?
– Mohit Tanwani
Aug 27 '16 at 7:56
Why did people downvote this answer?
– John Demetriou
Oct 23 '16 at 8:25
add a comment |
$a = 5; // 5 as an integer
var_dump($a == 5); // compare value; return true
var_dump($a == '5'); // compare value (ignore type); return true
var_dump($a === 5); // compare type/value (integer vs. integer); return true
var_dump($a === '5'); // compare type/value (integer vs. string); return false
Be careful though. Here is a notorious problem.
// 'test' is found at position 0, which is interpreted as the boolean 'false'
if (strpos('testing', 'test')) {
// code...
}
vs.
// true, as strict comparison was made (0 !== false)
if (strpos('testing', 'test') !== false) {
// code...
}
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:51
add a comment |
In short, === works in the same manner that == does in most other programming languages.
PHP allows you to make comparisons that don't really make sense. Example:
$y = "wauv";
$x = false;
if ($x == $y)
...
While this allows for some interesting "shortcuts" you should beware since a function that returns something it shouldn't (like "error" instead of a number) will not get caught, and you will be left wondering what happened.
In PHP, == compares values and performs type conversion if necessary (for instance, the string "12343sdfjskfjds" will become "12343" in an integer comparison). === will compare the value AND type and will return false if the type is not the same.
If you look in the PHP manual, you will see that a lot of functions return "false" if the function fails, but they might return 0 in a successful scenario, which is why they recommend doing "if (function() !== false)" to avoid mistakes.
1
It should be noted that in addition to those "shortcuts", the abnormal behavoir of the == operator has been known to open security holes, for example a popular PHP forum where it was possible to set the cookies password hash value to true, circumventing the if(databasehash==cookiehash) validation.
– David
Feb 26 '09 at 11:44
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:50
add a comment |
You would use === to test whether a function or variable is false rather than just equating to false (zero or an empty string).
$needle = 'a';
$haystack = 'abc';
$pos = strpos($haystack, $needle);
if ($pos === false) {
echo $needle . ' was not found in ' . $haystack;
} else {
echo $needle . ' was found in ' . $haystack . ' at location ' . $pos;
}
In this case strpos would return 0 which would equate to false in the test
if ($pos == false)
or
if (!$pos)
which is not what you want here.
add a comment |
As for when to use one over the other, take for example the fwrite()
function in PHP.
This function writes content to a file stream. According to PHP, "fwrite()
returns the number of bytes written, or FALSE on error.". If you want to test if the function call was successful, this method is flawed:
if (!fwrite(stuff))
{
log('error!');
}
It can return zero (and is considered successful), and your condition still gets triggered. The right way would be:
if (fwrite(stuff) === FALSE)
{
log('error!');
}
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:50
add a comment |
PHP is a loosely typed language. Using the double equal operator allows for a loose checking of a variable.
Loosely checking a value would allow for some similar, but not equal, values to equate as the same:
- ''
- null
- false
- 0
All of these values would equate as equal using the double equal operator.
add a comment |
Variables have a type and a value.
- $var = "test" is a string that contain "test"
- $var2 = 24 is an integer vhose value is 24.
When you use these variables (in PHP), sometimes you don't have the good type.
For example, if you do
if ($var == 1) {... do something ...}
PHP have to convert ("to cast") $var to integer. In this case, "$var == 1" is true because any non-empty string is casted to 1.
When using ===, you check that the value AND THE TYPE are equal, so "$var === 1" is false.
This is useful, for example, when you have a function that can return false (on error) and 0 (result) :
if(myFunction() == false) { ... error on myFunction ... }
This code is wrong as if myFunction()
returns 0, it is casted to false and you seem to have an error. The correct code is :
if(myFunction() === false) { ... error on myFunction ... }
because the test is that the return value "is a boolean and is false" and not "can be casted to false".
regarding non-empty strings, that's actually not true. "a" == 0 is TRUE.
– nickf
Sep 17 '08 at 7:56
add a comment |
The ===
operator is supposed to compare exact content equality while the ==
operator would compare semantic equality. In particular it will coerce strings to numbers.
Equality is a vast subject. See the Wikipedia article on equality.
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:51
add a comment |
<?php
/**
* Comparison of two PHP objects == ===
* Checks for
* 1. References yes yes
* 2. Instances with matching attributes and its values yes no
* 3. Instances with different attributes yes no
**/
// There is no need to worry about comparing visibility of property or
// method, because it will be the same whenever an object instance is
// created, however visibility of an object can be modified during run
// time using ReflectionClass()
// http://php.net/manual/en/reflectionproperty.setaccessible.php
//
class Foo
{
public $foobar = 1;
public function createNewProperty($name, $value)
{
$this->{$name} = $value;
}
}
class Bar
{
}
// 1. Object handles or references
// Is an object a reference to itself or a clone or totally a different object?
//
// == true Name of two objects are same, for example, Foo() and Foo()
// == false Name of two objects are different, for example, Foo() and Bar()
// === true ID of two objects are same, for example, 1 and 1
// === false ID of two objects are different, for example, 1 and 2
echo "1. Object handles or references (both == and ===) <br />";
$bar = new Foo(); // New object Foo() created
$bar2 = new Foo(); // New object Foo() created
$baz = clone $bar; // Object Foo() cloned
$qux = $bar; // Object Foo() referenced
$norf = new Bar(); // New object Bar() created
echo "bar";
var_dump($bar);
echo "baz";
var_dump($baz);
echo "qux";
var_dump($qux);
echo "bar2";
var_dump($bar2);
echo "norf";
var_dump($norf);
// Clone: == true and === false
echo '$bar == $bar2';
var_dump($bar == $bar2); // true
echo '$bar === $bar2';
var_dump($bar === $bar2); // false
echo '$bar == $baz';
var_dump($bar == $baz); // true
echo '$bar === $baz';
var_dump($bar === $baz); // false
// Object reference: == true and === true
echo '$bar == $qux';
var_dump($bar == $qux); // true
echo '$bar === $qux';
var_dump($bar === $qux); // true
// Two different objects: == false and === false
echo '$bar == $norf';
var_dump($bar == $norf); // false
echo '$bar === $norf';
var_dump($bar === $norf); // false
// 2. Instances with matching attributes and its values (only ==).
// What happens when objects (even in cloned object) have same
// attributes but varying values?
// $foobar value is different
echo "2. Instances with matching attributes and its values (only ==) <br />";
$baz->foobar = 2;
echo '$foobar' . " value is different <br />";
echo '$bar->foobar = ' . $bar->foobar . "<br />";
echo '$baz->foobar = ' . $baz->foobar . "<br />";
echo '$bar == $baz';
var_dump($bar == $baz); // false
// $foobar's value is the same again
$baz->foobar = 1;
echo '$foobar' . " value is the same again <br />";
echo '$bar->foobar is ' . $bar->foobar . "<br />";
echo '$baz->foobar is ' . $baz->foobar . "<br />";
echo '$bar == $baz';
var_dump($bar == $baz); // true
// Changing values of properties in $qux object will change the property
// value of $bar and evaluates true always, because $qux = &$bar.
$qux->foobar = 2;
echo '$foobar value of both $qux and $bar is 2, because $qux = &$bar' . "<br />";
echo '$qux->foobar is ' . $qux->foobar . "<br />";
echo '$bar->foobar is ' . $bar->foobar . "<br />";
echo '$bar == $qux';
var_dump($bar == $qux); // true
// 3. Instances with different attributes (only ==)
// What happens when objects have different attributes even though
// one of the attributes has same value?
echo "3. Instances with different attributes (only ==) <br />";
// Dynamically create a property with the name in $name and value
// in $value for baz object
$name = 'newproperty';
$value = null;
$baz->createNewProperty($name, $value);
echo '$baz->newproperty is ' . $baz->{$name};
var_dump($baz);
$baz->foobar = 2;
echo '$foobar' . " value is same again <br />";
echo '$bar->foobar is ' . $bar->foobar . "<br />";
echo '$baz->foobar is ' . $baz->foobar . "<br />";
echo '$bar == $baz';
var_dump($bar == $baz); // false
var_dump($bar);
var_dump($baz);
?>
add a comment |
All of the answers so far ignore a dangerous problem with ===. It has been noted in passing, but not stressed, that integer and double are different types, so the following code:
$n = 1000;
$d = $n + 0.0e0;
echo '<br/>'. ( ($n == $d)?'equal' :'not equal' );
echo '<br/>'. ( ($n === $d)?'equal' :'not equal' );
gives:
equal
not equal
Note that this is NOT a case of a "rounding error". The two numbers are exactly equal down to the last bit, but they have different types.
This is a nasty problem because a program using === can run happily for years if all of the numbers are small enough (where "small enough" depends on the hardware and OS you are running on). However, if by chance, an integer happens to be large enough to be converted to a double, its type is changed "forever" even though a subsequent operation, or many operations, might bring it back to a small integer in value. And, it gets worse. It can spread - double-ness infection can be passed along to anything it touches, one calculation at a time.
In the real world, this is likely to be a problem in programs that handle dates beyond the year 2038, for example. At this time, UNIX timestamps (number of seconds since 1970-01-01 00:00:00 UTC) will require more than 32-bits, so their representation will "magically" switch to double on some systems. Therefore, if you calculate the difference between two times you might end up with a couple of seconds, but as a double, rather than the integer result that occurs in the year 2017.
I think this is much worse than conversions between strings and numbers because it is subtle. I find it easy to keep track of what is a string and what is a number, but keeping track of the number of bits in a number is beyond me.
So, in the above answers there are some nice tables, but no distinction between 1 (as an integer) and 1 (subtle double) and 1.0 (obvious double). Also, advice that you should always use === and never == is not great because === will sometimes fail where == works properly. Also, JavaScript is not equivalent in this regard because it has only one number type (internally it may have different bit-wise representations, but it does not cause problems for ===).
My advice - use neither. You need to write your own comparison function to really fix this mess.
add a comment |
There are two differences between ==
and ===
in PHP arrays and objects that I think didn't mention here; two arrays with different key sorts, and objects.
Two arrays with different key sorts
If you have an array with a key sort and another array with a different key sort, they are strictly different (i.e. using ===
). That may cause if you key-sort an array, and try to compare the sorted array with the original one.
For instance, consider an empty array. First, we try to push some new indexes to the array without any special sort. A good example would be an array with strings as keys. Now deep into an example:
// Define an array
$arr = ;
// Adding unsorted keys
$arr["I"] = "we";
$arr["you"] = "you";
$arr["he"] = "they";
Now, we have a not-sorted-keys array (e.g., 'he' came after 'you'). Consider the same array, but we sorted its keys alphabetically:
// Declare array
$alphabetArr = ;
// Adding alphabetical-sorted keys
$alphabetArr["I"] = "we";
$alphabetArr["he"] = "they";
$alphabetArr["you"] = "you";
Tip: You can sort an array by key using ksort() function.
Now you have another array with a different key sort from the first one. So, we're going to compare them:
$arr == $alphabetArr; // true
$arr === $alphabetArr; // false
Note: It may be obvious, but comparing two different arrays using strict comparison always results false
. However, two arbitrary arrays may be equal using ===
or not.
You would say: "This difference is negligible". Then I say it's a difference and should be considered and may happen anytime. As mentioned above, sorting keys in an array is a good example of that.
Objects
Keep in mind, two different objects are never strict-equal. These examples would help:
$stdClass1 = new stdClass();
$stdClass2 = new stdClass();
$clonedStdClass1 = clone $stdClass1;
// Comparing
$stdClass1 == $stdClass2; // true
$stdClass1 === $stdClass2; // false
$stdClass1 == $clonedStdClass1; // true
$stdClass1 === $clonedStdClass1; // false
Note: Assigning an object to another variable does not create a copy - rather, it creates a reference to the same memory location as the object. See here.
Note: As of PHP7, anonymous classes was added. From the results, there is no difference between new class {}
and new stdClass()
in the tests above.
add a comment |
php == is a comparison operator which compares the value of the variables. But === compares the value and the data type.
For example,
<?php
$var1 = 10;
$var2 = '10';
if($var1 == $var2) {
echo 'Variables are equal';
} else {
echo 'Variables are not equal';
}
?>
In this case the output will be 'Variables are equal', even though their data types are different.
But if we use === instead of ==, the output will be 'Variables are not equal'. The php first compares the value of the variable and then the data type. Here the values are same, but data types are different.
add a comment |
protected by Shankar Damodaran Feb 22 '14 at 9:24
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
21 Answers
21
active
oldest
votes
21 Answers
21
active
oldest
votes
active
oldest
votes
active
oldest
votes
Difference between ==
and ===
The difference between the loosely ==
equal operator and the strict ===
identical operator is exactly explained in the manual:
Comparison Operators
┌──────────┬───────────┬───────────────────────────────────────────────────────────┐
│ 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. │
└──────────┴───────────┴───────────────────────────────────────────────────────────┘
Loosely ==
equal comparison
If you are using the ==
operator, or any other comparison operator which uses loosely comparison such as !=
, <>
or ==
, you always have to look at the context to see what, where and why something gets converted to understand what is going on.
Converting rules
- Converting to boolean
- Converting to integer
- Converting to float
- Converting to string
- Converting to array
- Converting to object
- Converting to resource
- Converting to NULL
Type comparison table
As reference and example you can see the comparison table in the manual:
Loose comparisons with
==
┌─────────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬─────────┬───────┬───────┐
│ │ TRUE │ FALSE │ 1 │ 0 │ -1 │ "1" │ "0" │ "-1" │ NULL │ array() │ "php" │ "" │
├─────────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼─────────┼───────┼───────┤
│ TRUE │ TRUE │ FALSE │ TRUE │ FALSE │ TRUE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │
│ FALSE │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ TRUE │ TRUE │ FALSE │ TRUE │
│ 1 │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ 0 │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ TRUE │ TRUE │
│ -1 │ TRUE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │
│ "1" │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ "0" │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ "-1" │ TRUE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │
│ NULL │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ TRUE │ FALSE │ TRUE │
│ array() │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ TRUE │ FALSE │ FALSE │
│ "php" │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │
│ "" │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │
└─────────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴─────────┴───────┴───────┘
Strict ===
identical comparison
If you are using the ===
operator, or any other comparison operator which uses strict comparison such as !==
or ===
, then you can always be sure that the types won't magically change, because there will be no converting going on. So with strict comparison the type and value have to be the same, not only the value.
Type comparison table
As reference and example you can see the comparison table in the manual:
Strict comparisons with
===
┌─────────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬─────────┬───────┬───────┐
│ │ TRUE │ FALSE │ 1 │ 0 │ -1 │ "1" │ "0" │ "-1" │ NULL │ array() │ "php" │ "" │
├─────────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼─────────┼───────┼───────┤
│ TRUE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ 1 │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ 0 │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ -1 │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ "1" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ "0" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ "-1" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │
│ NULL │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │
│ array() │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │
│ "php" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │
│ "" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │
└─────────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴─────────┴───────┴───────┘
60
This works much the same in Javascript, btw.
– Raithlin
Sep 17 '08 at 7:11
61
does anyone else find it strange that "000" == "0000" ?
– nickf
Sep 17 '08 at 13:08
35
What always suprises me is that false == array(), and false == 0 but array() != 0, so false == array() !=/== 0? that feels weird to me.
– Pim Jager
Aug 17 '09 at 11:50
11
@Raithlin, careful of array. triple equals givesfalse
for different arrays in javascript, buttrue
for PHP as long as their values are equal.
– Pacerier
Aug 2 '13 at 10:34
13
@Raithlin, Many many more gotchas. In JavaScript:"000" != "00"
,"000" == null
,"000" == false
,"0x0" == false
,array() == 0
,false != null
,array() != null
,false == "0x0"
,false == "000"
. In PHP, it's opposite behavior:"000" == "00"
,"000" != null
,"000" != false
,"0x0" != false
,array() != 0
,false == null
,array() == null
,false != "0x0"
,false != "000"
.
– Pacerier
Aug 7 '13 at 17:03
|
show 15 more comments
Difference between ==
and ===
The difference between the loosely ==
equal operator and the strict ===
identical operator is exactly explained in the manual:
Comparison Operators
┌──────────┬───────────┬───────────────────────────────────────────────────────────┐
│ 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. │
└──────────┴───────────┴───────────────────────────────────────────────────────────┘
Loosely ==
equal comparison
If you are using the ==
operator, or any other comparison operator which uses loosely comparison such as !=
, <>
or ==
, you always have to look at the context to see what, where and why something gets converted to understand what is going on.
Converting rules
- Converting to boolean
- Converting to integer
- Converting to float
- Converting to string
- Converting to array
- Converting to object
- Converting to resource
- Converting to NULL
Type comparison table
As reference and example you can see the comparison table in the manual:
Loose comparisons with
==
┌─────────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬─────────┬───────┬───────┐
│ │ TRUE │ FALSE │ 1 │ 0 │ -1 │ "1" │ "0" │ "-1" │ NULL │ array() │ "php" │ "" │
├─────────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼─────────┼───────┼───────┤
│ TRUE │ TRUE │ FALSE │ TRUE │ FALSE │ TRUE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │
│ FALSE │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ TRUE │ TRUE │ FALSE │ TRUE │
│ 1 │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ 0 │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ TRUE │ TRUE │
│ -1 │ TRUE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │
│ "1" │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ "0" │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ "-1" │ TRUE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │
│ NULL │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ TRUE │ FALSE │ TRUE │
│ array() │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ TRUE │ FALSE │ FALSE │
│ "php" │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │
│ "" │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │
└─────────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴─────────┴───────┴───────┘
Strict ===
identical comparison
If you are using the ===
operator, or any other comparison operator which uses strict comparison such as !==
or ===
, then you can always be sure that the types won't magically change, because there will be no converting going on. So with strict comparison the type and value have to be the same, not only the value.
Type comparison table
As reference and example you can see the comparison table in the manual:
Strict comparisons with
===
┌─────────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬─────────┬───────┬───────┐
│ │ TRUE │ FALSE │ 1 │ 0 │ -1 │ "1" │ "0" │ "-1" │ NULL │ array() │ "php" │ "" │
├─────────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼─────────┼───────┼───────┤
│ TRUE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ 1 │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ 0 │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ -1 │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ "1" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ "0" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ "-1" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │
│ NULL │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │
│ array() │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │
│ "php" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │
│ "" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │
└─────────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴─────────┴───────┴───────┘
60
This works much the same in Javascript, btw.
– Raithlin
Sep 17 '08 at 7:11
61
does anyone else find it strange that "000" == "0000" ?
– nickf
Sep 17 '08 at 13:08
35
What always suprises me is that false == array(), and false == 0 but array() != 0, so false == array() !=/== 0? that feels weird to me.
– Pim Jager
Aug 17 '09 at 11:50
11
@Raithlin, careful of array. triple equals givesfalse
for different arrays in javascript, buttrue
for PHP as long as their values are equal.
– Pacerier
Aug 2 '13 at 10:34
13
@Raithlin, Many many more gotchas. In JavaScript:"000" != "00"
,"000" == null
,"000" == false
,"0x0" == false
,array() == 0
,false != null
,array() != null
,false == "0x0"
,false == "000"
. In PHP, it's opposite behavior:"000" == "00"
,"000" != null
,"000" != false
,"0x0" != false
,array() != 0
,false == null
,array() == null
,false != "0x0"
,false != "000"
.
– Pacerier
Aug 7 '13 at 17:03
|
show 15 more comments
Difference between ==
and ===
The difference between the loosely ==
equal operator and the strict ===
identical operator is exactly explained in the manual:
Comparison Operators
┌──────────┬───────────┬───────────────────────────────────────────────────────────┐
│ 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. │
└──────────┴───────────┴───────────────────────────────────────────────────────────┘
Loosely ==
equal comparison
If you are using the ==
operator, or any other comparison operator which uses loosely comparison such as !=
, <>
or ==
, you always have to look at the context to see what, where and why something gets converted to understand what is going on.
Converting rules
- Converting to boolean
- Converting to integer
- Converting to float
- Converting to string
- Converting to array
- Converting to object
- Converting to resource
- Converting to NULL
Type comparison table
As reference and example you can see the comparison table in the manual:
Loose comparisons with
==
┌─────────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬─────────┬───────┬───────┐
│ │ TRUE │ FALSE │ 1 │ 0 │ -1 │ "1" │ "0" │ "-1" │ NULL │ array() │ "php" │ "" │
├─────────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼─────────┼───────┼───────┤
│ TRUE │ TRUE │ FALSE │ TRUE │ FALSE │ TRUE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │
│ FALSE │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ TRUE │ TRUE │ FALSE │ TRUE │
│ 1 │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ 0 │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ TRUE │ TRUE │
│ -1 │ TRUE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │
│ "1" │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ "0" │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ "-1" │ TRUE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │
│ NULL │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ TRUE │ FALSE │ TRUE │
│ array() │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ TRUE │ FALSE │ FALSE │
│ "php" │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │
│ "" │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │
└─────────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴─────────┴───────┴───────┘
Strict ===
identical comparison
If you are using the ===
operator, or any other comparison operator which uses strict comparison such as !==
or ===
, then you can always be sure that the types won't magically change, because there will be no converting going on. So with strict comparison the type and value have to be the same, not only the value.
Type comparison table
As reference and example you can see the comparison table in the manual:
Strict comparisons with
===
┌─────────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬─────────┬───────┬───────┐
│ │ TRUE │ FALSE │ 1 │ 0 │ -1 │ "1" │ "0" │ "-1" │ NULL │ array() │ "php" │ "" │
├─────────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼─────────┼───────┼───────┤
│ TRUE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ 1 │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ 0 │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ -1 │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ "1" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ "0" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ "-1" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │
│ NULL │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │
│ array() │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │
│ "php" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │
│ "" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │
└─────────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴─────────┴───────┴───────┘
Difference between ==
and ===
The difference between the loosely ==
equal operator and the strict ===
identical operator is exactly explained in the manual:
Comparison Operators
┌──────────┬───────────┬───────────────────────────────────────────────────────────┐
│ 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. │
└──────────┴───────────┴───────────────────────────────────────────────────────────┘
Loosely ==
equal comparison
If you are using the ==
operator, or any other comparison operator which uses loosely comparison such as !=
, <>
or ==
, you always have to look at the context to see what, where and why something gets converted to understand what is going on.
Converting rules
- Converting to boolean
- Converting to integer
- Converting to float
- Converting to string
- Converting to array
- Converting to object
- Converting to resource
- Converting to NULL
Type comparison table
As reference and example you can see the comparison table in the manual:
Loose comparisons with
==
┌─────────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬─────────┬───────┬───────┐
│ │ TRUE │ FALSE │ 1 │ 0 │ -1 │ "1" │ "0" │ "-1" │ NULL │ array() │ "php" │ "" │
├─────────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼─────────┼───────┼───────┤
│ TRUE │ TRUE │ FALSE │ TRUE │ FALSE │ TRUE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │
│ FALSE │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ TRUE │ TRUE │ FALSE │ TRUE │
│ 1 │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ 0 │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ TRUE │ TRUE │
│ -1 │ TRUE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │
│ "1" │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ "0" │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ "-1" │ TRUE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │
│ NULL │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ TRUE │ FALSE │ TRUE │
│ array() │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ TRUE │ FALSE │ FALSE │
│ "php" │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │
│ "" │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │
└─────────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴─────────┴───────┴───────┘
Strict ===
identical comparison
If you are using the ===
operator, or any other comparison operator which uses strict comparison such as !==
or ===
, then you can always be sure that the types won't magically change, because there will be no converting going on. So with strict comparison the type and value have to be the same, not only the value.
Type comparison table
As reference and example you can see the comparison table in the manual:
Strict comparisons with
===
┌─────────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬─────────┬───────┬───────┐
│ │ TRUE │ FALSE │ 1 │ 0 │ -1 │ "1" │ "0" │ "-1" │ NULL │ array() │ "php" │ "" │
├─────────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼─────────┼───────┼───────┤
│ TRUE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ 1 │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ 0 │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ -1 │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ "1" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ "0" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │
│ "-1" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │
│ NULL │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │
│ array() │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │
│ "php" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │
│ "" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │
└─────────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴─────────┴───────┴───────┘
edited Apr 13 '16 at 6:59


Rizier123
52.1k1563106
52.1k1563106
answered Sep 17 '08 at 6:57
nickfnickf
374k173585687
374k173585687
60
This works much the same in Javascript, btw.
– Raithlin
Sep 17 '08 at 7:11
61
does anyone else find it strange that "000" == "0000" ?
– nickf
Sep 17 '08 at 13:08
35
What always suprises me is that false == array(), and false == 0 but array() != 0, so false == array() !=/== 0? that feels weird to me.
– Pim Jager
Aug 17 '09 at 11:50
11
@Raithlin, careful of array. triple equals givesfalse
for different arrays in javascript, buttrue
for PHP as long as their values are equal.
– Pacerier
Aug 2 '13 at 10:34
13
@Raithlin, Many many more gotchas. In JavaScript:"000" != "00"
,"000" == null
,"000" == false
,"0x0" == false
,array() == 0
,false != null
,array() != null
,false == "0x0"
,false == "000"
. In PHP, it's opposite behavior:"000" == "00"
,"000" != null
,"000" != false
,"0x0" != false
,array() != 0
,false == null
,array() == null
,false != "0x0"
,false != "000"
.
– Pacerier
Aug 7 '13 at 17:03
|
show 15 more comments
60
This works much the same in Javascript, btw.
– Raithlin
Sep 17 '08 at 7:11
61
does anyone else find it strange that "000" == "0000" ?
– nickf
Sep 17 '08 at 13:08
35
What always suprises me is that false == array(), and false == 0 but array() != 0, so false == array() !=/== 0? that feels weird to me.
– Pim Jager
Aug 17 '09 at 11:50
11
@Raithlin, careful of array. triple equals givesfalse
for different arrays in javascript, buttrue
for PHP as long as their values are equal.
– Pacerier
Aug 2 '13 at 10:34
13
@Raithlin, Many many more gotchas. In JavaScript:"000" != "00"
,"000" == null
,"000" == false
,"0x0" == false
,array() == 0
,false != null
,array() != null
,false == "0x0"
,false == "000"
. In PHP, it's opposite behavior:"000" == "00"
,"000" != null
,"000" != false
,"0x0" != false
,array() != 0
,false == null
,array() == null
,false != "0x0"
,false != "000"
.
– Pacerier
Aug 7 '13 at 17:03
60
60
This works much the same in Javascript, btw.
– Raithlin
Sep 17 '08 at 7:11
This works much the same in Javascript, btw.
– Raithlin
Sep 17 '08 at 7:11
61
61
does anyone else find it strange that "000" == "0000" ?
– nickf
Sep 17 '08 at 13:08
does anyone else find it strange that "000" == "0000" ?
– nickf
Sep 17 '08 at 13:08
35
35
What always suprises me is that false == array(), and false == 0 but array() != 0, so false == array() !=/== 0? that feels weird to me.
– Pim Jager
Aug 17 '09 at 11:50
What always suprises me is that false == array(), and false == 0 but array() != 0, so false == array() !=/== 0? that feels weird to me.
– Pim Jager
Aug 17 '09 at 11:50
11
11
@Raithlin, careful of array. triple equals gives
false
for different arrays in javascript, but true
for PHP as long as their values are equal.– Pacerier
Aug 2 '13 at 10:34
@Raithlin, careful of array. triple equals gives
false
for different arrays in javascript, but true
for PHP as long as their values are equal.– Pacerier
Aug 2 '13 at 10:34
13
13
@Raithlin, Many many more gotchas. In JavaScript:
"000" != "00"
, "000" == null
, "000" == false
, "0x0" == false
, array() == 0
, false != null
, array() != null
, false == "0x0"
, false == "000"
. In PHP, it's opposite behavior: "000" == "00"
, "000" != null
, "000" != false
, "0x0" != false
, array() != 0
, false == null
, array() == null
, false != "0x0"
, false != "000"
.– Pacerier
Aug 7 '13 at 17:03
@Raithlin, Many many more gotchas. In JavaScript:
"000" != "00"
, "000" == null
, "000" == false
, "0x0" == false
, array() == 0
, false != null
, array() != null
, false == "0x0"
, false == "000"
. In PHP, it's opposite behavior: "000" == "00"
, "000" != null
, "000" != false
, "0x0" != false
, array() != 0
, false == null
, array() == null
, false != "0x0"
, false != "000"
.– Pacerier
Aug 7 '13 at 17:03
|
show 15 more comments
The operator == casts between two different types if they are different, while the === operator performs a 'typesafe comparison'. That means that it will only return true if both operands have the same type and the same value.
Examples:
1 === 1: true
1 == 1: true
1 === "1": false // 1 is an integer, "1" is a string
1 == "1": true // "1" gets casted to an integer, which is 1
"foo" === "foo": true // both operands are strings and have the same value
Warning: two instances of the same class with equivalent members do NOT match the ===
operator. Example:
$a = new stdClass();
$a->foo = "bar";
$b = clone $a;
var_dump($a === $b); // bool(false)
3
Nitpick: === will only return true if both operands are the same type and the values are equal =)
– gnud
Feb 26 '09 at 9:16
1
@gnud That's exactly what he's shown in the example. If it was just comparing the types it would just be called a "type comparison" wouldn't it.
– Rob Stevenson-Leggett
Feb 26 '09 at 9:34
3
After using PHP for 8 years, yesterday was the first time I got caught in a situation where I should've used ===
– uuɐɯǝʃǝs
Apr 13 '09 at 13:21
3
=== true if they are equal and have same type. == true if they are equal. != true if they aren't equal. !== true if either they aren't equal, or are equal but aren't the same type.
– Jeremy C
Oct 22 '12 at 23:55
Also, using === is slightly faster than == since it doesn't need to convert the value before checking if it's equal.
– clauziere
Mar 18 '14 at 20:04
|
show 3 more comments
The operator == casts between two different types if they are different, while the === operator performs a 'typesafe comparison'. That means that it will only return true if both operands have the same type and the same value.
Examples:
1 === 1: true
1 == 1: true
1 === "1": false // 1 is an integer, "1" is a string
1 == "1": true // "1" gets casted to an integer, which is 1
"foo" === "foo": true // both operands are strings and have the same value
Warning: two instances of the same class with equivalent members do NOT match the ===
operator. Example:
$a = new stdClass();
$a->foo = "bar";
$b = clone $a;
var_dump($a === $b); // bool(false)
3
Nitpick: === will only return true if both operands are the same type and the values are equal =)
– gnud
Feb 26 '09 at 9:16
1
@gnud That's exactly what he's shown in the example. If it was just comparing the types it would just be called a "type comparison" wouldn't it.
– Rob Stevenson-Leggett
Feb 26 '09 at 9:34
3
After using PHP for 8 years, yesterday was the first time I got caught in a situation where I should've used ===
– uuɐɯǝʃǝs
Apr 13 '09 at 13:21
3
=== true if they are equal and have same type. == true if they are equal. != true if they aren't equal. !== true if either they aren't equal, or are equal but aren't the same type.
– Jeremy C
Oct 22 '12 at 23:55
Also, using === is slightly faster than == since it doesn't need to convert the value before checking if it's equal.
– clauziere
Mar 18 '14 at 20:04
|
show 3 more comments
The operator == casts between two different types if they are different, while the === operator performs a 'typesafe comparison'. That means that it will only return true if both operands have the same type and the same value.
Examples:
1 === 1: true
1 == 1: true
1 === "1": false // 1 is an integer, "1" is a string
1 == "1": true // "1" gets casted to an integer, which is 1
"foo" === "foo": true // both operands are strings and have the same value
Warning: two instances of the same class with equivalent members do NOT match the ===
operator. Example:
$a = new stdClass();
$a->foo = "bar";
$b = clone $a;
var_dump($a === $b); // bool(false)
The operator == casts between two different types if they are different, while the === operator performs a 'typesafe comparison'. That means that it will only return true if both operands have the same type and the same value.
Examples:
1 === 1: true
1 == 1: true
1 === "1": false // 1 is an integer, "1" is a string
1 == "1": true // "1" gets casted to an integer, which is 1
"foo" === "foo": true // both operands are strings and have the same value
Warning: two instances of the same class with equivalent members do NOT match the ===
operator. Example:
$a = new stdClass();
$a->foo = "bar";
$b = clone $a;
var_dump($a === $b); // bool(false)
edited Mar 12 '17 at 21:10
answered Feb 26 '09 at 7:52
Patrick GlandienPatrick Glandien
6,44053345
6,44053345
3
Nitpick: === will only return true if both operands are the same type and the values are equal =)
– gnud
Feb 26 '09 at 9:16
1
@gnud That's exactly what he's shown in the example. If it was just comparing the types it would just be called a "type comparison" wouldn't it.
– Rob Stevenson-Leggett
Feb 26 '09 at 9:34
3
After using PHP for 8 years, yesterday was the first time I got caught in a situation where I should've used ===
– uuɐɯǝʃǝs
Apr 13 '09 at 13:21
3
=== true if they are equal and have same type. == true if they are equal. != true if they aren't equal. !== true if either they aren't equal, or are equal but aren't the same type.
– Jeremy C
Oct 22 '12 at 23:55
Also, using === is slightly faster than == since it doesn't need to convert the value before checking if it's equal.
– clauziere
Mar 18 '14 at 20:04
|
show 3 more comments
3
Nitpick: === will only return true if both operands are the same type and the values are equal =)
– gnud
Feb 26 '09 at 9:16
1
@gnud That's exactly what he's shown in the example. If it was just comparing the types it would just be called a "type comparison" wouldn't it.
– Rob Stevenson-Leggett
Feb 26 '09 at 9:34
3
After using PHP for 8 years, yesterday was the first time I got caught in a situation where I should've used ===
– uuɐɯǝʃǝs
Apr 13 '09 at 13:21
3
=== true if they are equal and have same type. == true if they are equal. != true if they aren't equal. !== true if either they aren't equal, or are equal but aren't the same type.
– Jeremy C
Oct 22 '12 at 23:55
Also, using === is slightly faster than == since it doesn't need to convert the value before checking if it's equal.
– clauziere
Mar 18 '14 at 20:04
3
3
Nitpick: === will only return true if both operands are the same type and the values are equal =)
– gnud
Feb 26 '09 at 9:16
Nitpick: === will only return true if both operands are the same type and the values are equal =)
– gnud
Feb 26 '09 at 9:16
1
1
@gnud That's exactly what he's shown in the example. If it was just comparing the types it would just be called a "type comparison" wouldn't it.
– Rob Stevenson-Leggett
Feb 26 '09 at 9:34
@gnud That's exactly what he's shown in the example. If it was just comparing the types it would just be called a "type comparison" wouldn't it.
– Rob Stevenson-Leggett
Feb 26 '09 at 9:34
3
3
After using PHP for 8 years, yesterday was the first time I got caught in a situation where I should've used ===
– uuɐɯǝʃǝs
Apr 13 '09 at 13:21
After using PHP for 8 years, yesterday was the first time I got caught in a situation where I should've used ===
– uuɐɯǝʃǝs
Apr 13 '09 at 13:21
3
3
=== true if they are equal and have same type. == true if they are equal. != true if they aren't equal. !== true if either they aren't equal, or are equal but aren't the same type.
– Jeremy C
Oct 22 '12 at 23:55
=== true if they are equal and have same type. == true if they are equal. != true if they aren't equal. !== true if either they aren't equal, or are equal but aren't the same type.
– Jeremy C
Oct 22 '12 at 23:55
Also, using === is slightly faster than == since it doesn't need to convert the value before checking if it's equal.
– clauziere
Mar 18 '14 at 20:04
Also, using === is slightly faster than == since it doesn't need to convert the value before checking if it's equal.
– clauziere
Mar 18 '14 at 20:04
|
show 3 more comments
A picture is worth a thousand words:
PHP Double Equals ==
equality chart:
PHP Triple Equals ===
Equality chart:
Source code to create these images:
https://github.com/sentientmachine/php_equality_charts
Guru Meditation
Those who wish to keep their sanity, read no further.
- '==' converts left and right operands to numbers when possible (123 == "123foo", but "123" != "123foo"
- A hex string in quotes is occasionally a float and will be casted to it against your will.
- == is not transitive because ("0" is == to 0, and 0 is == to "" but "0" != "")
- "6" == " 6", "4.2" == "4.20", and "133" == "0133". But 133 != 0133, because 0133 is octal. But "0x10" == "16" and "1e3" == "1000"
PHP Variables that have not been declared yet are false.
False is equal to 0, blankstring and empty array and "0".
- When numbers are big enough they are == Infinity.
NAN does not == itself, but it is True.
A fresh class is == to 1.
- False is the most dangerous value because False is == to most of the other variables, mostly defeating it's purpose.
Hope:
If you are using PHP, Thou shalt not use the double equals operator, always use triple equals.
Is it really a good idea (also secure) to always use triple equals?
– Chazy Chaz
Jan 16 '17 at 22:42
2
Yes, the transitive property of triple equals makes it more secure and webscale.
– Eric Leschinski
Jan 16 '17 at 23:55
add a comment |
A picture is worth a thousand words:
PHP Double Equals ==
equality chart:
PHP Triple Equals ===
Equality chart:
Source code to create these images:
https://github.com/sentientmachine/php_equality_charts
Guru Meditation
Those who wish to keep their sanity, read no further.
- '==' converts left and right operands to numbers when possible (123 == "123foo", but "123" != "123foo"
- A hex string in quotes is occasionally a float and will be casted to it against your will.
- == is not transitive because ("0" is == to 0, and 0 is == to "" but "0" != "")
- "6" == " 6", "4.2" == "4.20", and "133" == "0133". But 133 != 0133, because 0133 is octal. But "0x10" == "16" and "1e3" == "1000"
PHP Variables that have not been declared yet are false.
False is equal to 0, blankstring and empty array and "0".
- When numbers are big enough they are == Infinity.
NAN does not == itself, but it is True.
A fresh class is == to 1.
- False is the most dangerous value because False is == to most of the other variables, mostly defeating it's purpose.
Hope:
If you are using PHP, Thou shalt not use the double equals operator, always use triple equals.
Is it really a good idea (also secure) to always use triple equals?
– Chazy Chaz
Jan 16 '17 at 22:42
2
Yes, the transitive property of triple equals makes it more secure and webscale.
– Eric Leschinski
Jan 16 '17 at 23:55
add a comment |
A picture is worth a thousand words:
PHP Double Equals ==
equality chart:
PHP Triple Equals ===
Equality chart:
Source code to create these images:
https://github.com/sentientmachine/php_equality_charts
Guru Meditation
Those who wish to keep their sanity, read no further.
- '==' converts left and right operands to numbers when possible (123 == "123foo", but "123" != "123foo"
- A hex string in quotes is occasionally a float and will be casted to it against your will.
- == is not transitive because ("0" is == to 0, and 0 is == to "" but "0" != "")
- "6" == " 6", "4.2" == "4.20", and "133" == "0133". But 133 != 0133, because 0133 is octal. But "0x10" == "16" and "1e3" == "1000"
PHP Variables that have not been declared yet are false.
False is equal to 0, blankstring and empty array and "0".
- When numbers are big enough they are == Infinity.
NAN does not == itself, but it is True.
A fresh class is == to 1.
- False is the most dangerous value because False is == to most of the other variables, mostly defeating it's purpose.
Hope:
If you are using PHP, Thou shalt not use the double equals operator, always use triple equals.
A picture is worth a thousand words:
PHP Double Equals ==
equality chart:
PHP Triple Equals ===
Equality chart:
Source code to create these images:
https://github.com/sentientmachine/php_equality_charts
Guru Meditation
Those who wish to keep their sanity, read no further.
- '==' converts left and right operands to numbers when possible (123 == "123foo", but "123" != "123foo"
- A hex string in quotes is occasionally a float and will be casted to it against your will.
- == is not transitive because ("0" is == to 0, and 0 is == to "" but "0" != "")
- "6" == " 6", "4.2" == "4.20", and "133" == "0133". But 133 != 0133, because 0133 is octal. But "0x10" == "16" and "1e3" == "1000"
PHP Variables that have not been declared yet are false.
False is equal to 0, blankstring and empty array and "0".
- When numbers are big enough they are == Infinity.
NAN does not == itself, but it is True.
A fresh class is == to 1.
- False is the most dangerous value because False is == to most of the other variables, mostly defeating it's purpose.
Hope:
If you are using PHP, Thou shalt not use the double equals operator, always use triple equals.
edited Feb 2 '17 at 12:37
lord_t
2,06022146
2,06022146
answered Nov 3 '16 at 0:42
Eric LeschinskiEric Leschinski
87.8k38323275
87.8k38323275
Is it really a good idea (also secure) to always use triple equals?
– Chazy Chaz
Jan 16 '17 at 22:42
2
Yes, the transitive property of triple equals makes it more secure and webscale.
– Eric Leschinski
Jan 16 '17 at 23:55
add a comment |
Is it really a good idea (also secure) to always use triple equals?
– Chazy Chaz
Jan 16 '17 at 22:42
2
Yes, the transitive property of triple equals makes it more secure and webscale.
– Eric Leschinski
Jan 16 '17 at 23:55
Is it really a good idea (also secure) to always use triple equals?
– Chazy Chaz
Jan 16 '17 at 22:42
Is it really a good idea (also secure) to always use triple equals?
– Chazy Chaz
Jan 16 '17 at 22:42
2
2
Yes, the transitive property of triple equals makes it more secure and webscale.
– Eric Leschinski
Jan 16 '17 at 23:55
Yes, the transitive property of triple equals makes it more secure and webscale.
– Eric Leschinski
Jan 16 '17 at 23:55
add a comment |
In regards to JavaScript:
The === operator works the same as the == operator, but it requires that its operands have not only the same value, but also the same data type.
For example, the sample below will display 'x and y are equal', but not 'x and y are identical'.
var x = 4;
var y = '4';
if (x == y) {
alert('x and y are equal');
}
if (x === y) {
alert('x and y are identical');
}
Upvoted, as this seems to be exactly the same situation for php.
– David Thomas
Aug 17 '09 at 13:40
1
@DavidThomas It's not exactly the same.See stackoverflow.com/questions/12598407/…
– xdazz
May 4 '13 at 12:45
add a comment |
In regards to JavaScript:
The === operator works the same as the == operator, but it requires that its operands have not only the same value, but also the same data type.
For example, the sample below will display 'x and y are equal', but not 'x and y are identical'.
var x = 4;
var y = '4';
if (x == y) {
alert('x and y are equal');
}
if (x === y) {
alert('x and y are identical');
}
Upvoted, as this seems to be exactly the same situation for php.
– David Thomas
Aug 17 '09 at 13:40
1
@DavidThomas It's not exactly the same.See stackoverflow.com/questions/12598407/…
– xdazz
May 4 '13 at 12:45
add a comment |
In regards to JavaScript:
The === operator works the same as the == operator, but it requires that its operands have not only the same value, but also the same data type.
For example, the sample below will display 'x and y are equal', but not 'x and y are identical'.
var x = 4;
var y = '4';
if (x == y) {
alert('x and y are equal');
}
if (x === y) {
alert('x and y are identical');
}
In regards to JavaScript:
The === operator works the same as the == operator, but it requires that its operands have not only the same value, but also the same data type.
For example, the sample below will display 'x and y are equal', but not 'x and y are identical'.
var x = 4;
var y = '4';
if (x == y) {
alert('x and y are equal');
}
if (x === y) {
alert('x and y are identical');
}
edited Jan 3 '16 at 15:41


Peter Mortensen
13.7k1986111
13.7k1986111
answered Sep 17 '08 at 7:27
user1684
Upvoted, as this seems to be exactly the same situation for php.
– David Thomas
Aug 17 '09 at 13:40
1
@DavidThomas It's not exactly the same.See stackoverflow.com/questions/12598407/…
– xdazz
May 4 '13 at 12:45
add a comment |
Upvoted, as this seems to be exactly the same situation for php.
– David Thomas
Aug 17 '09 at 13:40
1
@DavidThomas It's not exactly the same.See stackoverflow.com/questions/12598407/…
– xdazz
May 4 '13 at 12:45
Upvoted, as this seems to be exactly the same situation for php.
– David Thomas
Aug 17 '09 at 13:40
Upvoted, as this seems to be exactly the same situation for php.
– David Thomas
Aug 17 '09 at 13:40
1
1
@DavidThomas It's not exactly the same.See stackoverflow.com/questions/12598407/…
– xdazz
May 4 '13 at 12:45
@DavidThomas It's not exactly the same.See stackoverflow.com/questions/12598407/…
– xdazz
May 4 '13 at 12:45
add a comment |
An addition to the other answers concerning object comparison:
== compares objects using the name of the object and their values. If two objects are of the same type and have the same member values, $a == $b
yields true.
=== compares the internal object id of the objects. Even if the members are equal, $a !== $b
if they are not exactly the same object.
class TestClassA {
public $a;
}
class TestClassB {
public $a;
}
$a1 = new TestClassA();
$a2 = new TestClassA();
$b = new TestClassB();
$a1->a = 10;
$a2->a = 10;
$b->a = 10;
$a1 == $a1;
$a1 == $a2; // Same members
$a1 != $b; // Different classes
$a1 === $a1;
$a1 !== $a2; // Not the same object
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:50
add a comment |
An addition to the other answers concerning object comparison:
== compares objects using the name of the object and their values. If two objects are of the same type and have the same member values, $a == $b
yields true.
=== compares the internal object id of the objects. Even if the members are equal, $a !== $b
if they are not exactly the same object.
class TestClassA {
public $a;
}
class TestClassB {
public $a;
}
$a1 = new TestClassA();
$a2 = new TestClassA();
$b = new TestClassB();
$a1->a = 10;
$a2->a = 10;
$b->a = 10;
$a1 == $a1;
$a1 == $a2; // Same members
$a1 != $b; // Different classes
$a1 === $a1;
$a1 !== $a2; // Not the same object
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:50
add a comment |
An addition to the other answers concerning object comparison:
== compares objects using the name of the object and their values. If two objects are of the same type and have the same member values, $a == $b
yields true.
=== compares the internal object id of the objects. Even if the members are equal, $a !== $b
if they are not exactly the same object.
class TestClassA {
public $a;
}
class TestClassB {
public $a;
}
$a1 = new TestClassA();
$a2 = new TestClassA();
$b = new TestClassB();
$a1->a = 10;
$a2->a = 10;
$b->a = 10;
$a1 == $a1;
$a1 == $a2; // Same members
$a1 != $b; // Different classes
$a1 === $a1;
$a1 !== $a2; // Not the same object
An addition to the other answers concerning object comparison:
== compares objects using the name of the object and their values. If two objects are of the same type and have the same member values, $a == $b
yields true.
=== compares the internal object id of the objects. Even if the members are equal, $a !== $b
if they are not exactly the same object.
class TestClassA {
public $a;
}
class TestClassB {
public $a;
}
$a1 = new TestClassA();
$a2 = new TestClassA();
$b = new TestClassB();
$a1->a = 10;
$a2->a = 10;
$b->a = 10;
$a1 == $a1;
$a1 == $a2; // Same members
$a1 != $b; // Different classes
$a1 === $a1;
$a1 !== $a2; // Not the same object
edited Aug 16 '10 at 8:56
answered Feb 26 '09 at 9:12
soulmergesoulmerge
60.9k15103142
60.9k15103142
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:50
add a comment |
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:50
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:50
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:50
add a comment |
In simplest terms:
== checks if equivalent (value only)
=== checks if the same (value && type)
Equivalent vs. Same: An Analogy
1 + 1 = 2 + 0 (equivalent)
1 + 1 = 1 + 1 (same)
In PHP:
true == 1 (true - equivalent in value)
true === 1 (false - not the same in value && type)
- true is boolean
- 1 is int
"=== checks if the same (value && type)", not exactly true. Two stdClass objects has the same type of 'object' (i.e. using gettype()), but PHP says they're two different things if you use strict comparison. See this.
– MAChitgarha
Sep 12 '18 at 16:44
add a comment |
In simplest terms:
== checks if equivalent (value only)
=== checks if the same (value && type)
Equivalent vs. Same: An Analogy
1 + 1 = 2 + 0 (equivalent)
1 + 1 = 1 + 1 (same)
In PHP:
true == 1 (true - equivalent in value)
true === 1 (false - not the same in value && type)
- true is boolean
- 1 is int
"=== checks if the same (value && type)", not exactly true. Two stdClass objects has the same type of 'object' (i.e. using gettype()), but PHP says they're two different things if you use strict comparison. See this.
– MAChitgarha
Sep 12 '18 at 16:44
add a comment |
In simplest terms:
== checks if equivalent (value only)
=== checks if the same (value && type)
Equivalent vs. Same: An Analogy
1 + 1 = 2 + 0 (equivalent)
1 + 1 = 1 + 1 (same)
In PHP:
true == 1 (true - equivalent in value)
true === 1 (false - not the same in value && type)
- true is boolean
- 1 is int
In simplest terms:
== checks if equivalent (value only)
=== checks if the same (value && type)
Equivalent vs. Same: An Analogy
1 + 1 = 2 + 0 (equivalent)
1 + 1 = 1 + 1 (same)
In PHP:
true == 1 (true - equivalent in value)
true === 1 (false - not the same in value && type)
- true is boolean
- 1 is int
edited Jun 5 '15 at 23:21
answered Feb 19 '15 at 9:52
silversilver
2,71353367
2,71353367
"=== checks if the same (value && type)", not exactly true. Two stdClass objects has the same type of 'object' (i.e. using gettype()), but PHP says they're two different things if you use strict comparison. See this.
– MAChitgarha
Sep 12 '18 at 16:44
add a comment |
"=== checks if the same (value && type)", not exactly true. Two stdClass objects has the same type of 'object' (i.e. using gettype()), but PHP says they're two different things if you use strict comparison. See this.
– MAChitgarha
Sep 12 '18 at 16:44
"=== checks if the same (value && type)", not exactly true. Two stdClass objects has the same type of 'object' (i.e. using gettype()), but PHP says they're two different things if you use strict comparison. See this.
– MAChitgarha
Sep 12 '18 at 16:44
"=== checks if the same (value && type)", not exactly true. Two stdClass objects has the same type of 'object' (i.e. using gettype()), but PHP says they're two different things if you use strict comparison. See this.
– MAChitgarha
Sep 12 '18 at 16:44
add a comment |
It's all about data types. Take a BOOL
(true or false) for example:
true
also equals 1
and
false
also equals 0
The ==
does not care about the data types when comparing:
So if you had a variable that is 1 (which could also be true
):
$var=1;
And then compare with the ==
:
if ($var == true)
{
echo"var is true";
}
But $var
does not actually equal true
, does it? It has the int value of 1
instead, which in turn, is equal to true.
With ===
, the data types are checked to make sure the two variables/objects/whatever are using the same type.
So if I did
if ($var === true)
{
echo "var is true";
}
that condition would not be true, as $var !== true
it only == true
(if you know what I mean).
Why would you need this?
Simple - let's take a look at one of PHP's functions: array_search()
:
The array_search()
function simply searches for a value in an array, and returns the key of the element the value was found in. If the value could not be found in the array, it returns false. But, what if you did an array_search()
on a value that was stored in the first element of the array (which would have the array key of 0
)....the array_search()
function would return 0...which is equal to false..
So if you did:
$arr = array("name");
if (array_search("name", $arr) == false)
{
// This would return 0 (the key of the element the val was found
// in), but because we're using ==, we'll think the function
// actually returned false...when it didn't.
}
So, do you see how this could be an issue now?
Most people don't use == false
when checking if a function returns false. Instead, they use the !
. But actually, this is exactly the same as using ==false
, so if you did:
$arr = array("name");
if (!array_search("name", $arr)) // This is the same as doing (array_search("name", $arr) == false)
So for things like that, you would use the ===
instead, so that the data type is checked.
add a comment |
It's all about data types. Take a BOOL
(true or false) for example:
true
also equals 1
and
false
also equals 0
The ==
does not care about the data types when comparing:
So if you had a variable that is 1 (which could also be true
):
$var=1;
And then compare with the ==
:
if ($var == true)
{
echo"var is true";
}
But $var
does not actually equal true
, does it? It has the int value of 1
instead, which in turn, is equal to true.
With ===
, the data types are checked to make sure the two variables/objects/whatever are using the same type.
So if I did
if ($var === true)
{
echo "var is true";
}
that condition would not be true, as $var !== true
it only == true
(if you know what I mean).
Why would you need this?
Simple - let's take a look at one of PHP's functions: array_search()
:
The array_search()
function simply searches for a value in an array, and returns the key of the element the value was found in. If the value could not be found in the array, it returns false. But, what if you did an array_search()
on a value that was stored in the first element of the array (which would have the array key of 0
)....the array_search()
function would return 0...which is equal to false..
So if you did:
$arr = array("name");
if (array_search("name", $arr) == false)
{
// This would return 0 (the key of the element the val was found
// in), but because we're using ==, we'll think the function
// actually returned false...when it didn't.
}
So, do you see how this could be an issue now?
Most people don't use == false
when checking if a function returns false. Instead, they use the !
. But actually, this is exactly the same as using ==false
, so if you did:
$arr = array("name");
if (!array_search("name", $arr)) // This is the same as doing (array_search("name", $arr) == false)
So for things like that, you would use the ===
instead, so that the data type is checked.
add a comment |
It's all about data types. Take a BOOL
(true or false) for example:
true
also equals 1
and
false
also equals 0
The ==
does not care about the data types when comparing:
So if you had a variable that is 1 (which could also be true
):
$var=1;
And then compare with the ==
:
if ($var == true)
{
echo"var is true";
}
But $var
does not actually equal true
, does it? It has the int value of 1
instead, which in turn, is equal to true.
With ===
, the data types are checked to make sure the two variables/objects/whatever are using the same type.
So if I did
if ($var === true)
{
echo "var is true";
}
that condition would not be true, as $var !== true
it only == true
(if you know what I mean).
Why would you need this?
Simple - let's take a look at one of PHP's functions: array_search()
:
The array_search()
function simply searches for a value in an array, and returns the key of the element the value was found in. If the value could not be found in the array, it returns false. But, what if you did an array_search()
on a value that was stored in the first element of the array (which would have the array key of 0
)....the array_search()
function would return 0...which is equal to false..
So if you did:
$arr = array("name");
if (array_search("name", $arr) == false)
{
// This would return 0 (the key of the element the val was found
// in), but because we're using ==, we'll think the function
// actually returned false...when it didn't.
}
So, do you see how this could be an issue now?
Most people don't use == false
when checking if a function returns false. Instead, they use the !
. But actually, this is exactly the same as using ==false
, so if you did:
$arr = array("name");
if (!array_search("name", $arr)) // This is the same as doing (array_search("name", $arr) == false)
So for things like that, you would use the ===
instead, so that the data type is checked.
It's all about data types. Take a BOOL
(true or false) for example:
true
also equals 1
and
false
also equals 0
The ==
does not care about the data types when comparing:
So if you had a variable that is 1 (which could also be true
):
$var=1;
And then compare with the ==
:
if ($var == true)
{
echo"var is true";
}
But $var
does not actually equal true
, does it? It has the int value of 1
instead, which in turn, is equal to true.
With ===
, the data types are checked to make sure the two variables/objects/whatever are using the same type.
So if I did
if ($var === true)
{
echo "var is true";
}
that condition would not be true, as $var !== true
it only == true
(if you know what I mean).
Why would you need this?
Simple - let's take a look at one of PHP's functions: array_search()
:
The array_search()
function simply searches for a value in an array, and returns the key of the element the value was found in. If the value could not be found in the array, it returns false. But, what if you did an array_search()
on a value that was stored in the first element of the array (which would have the array key of 0
)....the array_search()
function would return 0...which is equal to false..
So if you did:
$arr = array("name");
if (array_search("name", $arr) == false)
{
// This would return 0 (the key of the element the val was found
// in), but because we're using ==, we'll think the function
// actually returned false...when it didn't.
}
So, do you see how this could be an issue now?
Most people don't use == false
when checking if a function returns false. Instead, they use the !
. But actually, this is exactly the same as using ==false
, so if you did:
$arr = array("name");
if (!array_search("name", $arr)) // This is the same as doing (array_search("name", $arr) == false)
So for things like that, you would use the ===
instead, so that the data type is checked.
edited Jan 3 '16 at 15:51


Peter Mortensen
13.7k1986111
13.7k1986111
answered Jul 18 '12 at 16:08
user849137
add a comment |
add a comment |
One example is that a database attribute can be null or "":
$attributeFromArray = "";
if ($attributeFromArray == ""){} //true
if ($attributeFromArray === ""){} //true
if ($attributeFromArray == null){} //true
if ($attributeFromArray === null){} //false
$attributeFromArray = null;
if ($attributeFromArray == ""){} //true
if ($attributeFromArray === ""){} //false
if ($attributeFromArray == null){} //true
if ($attributeFromArray === null){} //true
add a comment |
One example is that a database attribute can be null or "":
$attributeFromArray = "";
if ($attributeFromArray == ""){} //true
if ($attributeFromArray === ""){} //true
if ($attributeFromArray == null){} //true
if ($attributeFromArray === null){} //false
$attributeFromArray = null;
if ($attributeFromArray == ""){} //true
if ($attributeFromArray === ""){} //false
if ($attributeFromArray == null){} //true
if ($attributeFromArray === null){} //true
add a comment |
One example is that a database attribute can be null or "":
$attributeFromArray = "";
if ($attributeFromArray == ""){} //true
if ($attributeFromArray === ""){} //true
if ($attributeFromArray == null){} //true
if ($attributeFromArray === null){} //false
$attributeFromArray = null;
if ($attributeFromArray == ""){} //true
if ($attributeFromArray === ""){} //false
if ($attributeFromArray == null){} //true
if ($attributeFromArray === null){} //true
One example is that a database attribute can be null or "":
$attributeFromArray = "";
if ($attributeFromArray == ""){} //true
if ($attributeFromArray === ""){} //true
if ($attributeFromArray == null){} //true
if ($attributeFromArray === null){} //false
$attributeFromArray = null;
if ($attributeFromArray == ""){} //true
if ($attributeFromArray === ""){} //false
if ($attributeFromArray == null){} //true
if ($attributeFromArray === null){} //true
edited Jan 3 '16 at 15:58


Peter Mortensen
13.7k1986111
13.7k1986111
answered Oct 14 '15 at 13:09


fico7489fico7489
2,82522143
2,82522143
add a comment |
add a comment |
Given x = 5
1) Operator : == is "equal to". x == 8
is false
2) Operator : === is "exactly equal to" (value and type) x === 5
is true, x === "5"
is false
add a comment |
Given x = 5
1) Operator : == is "equal to". x == 8
is false
2) Operator : === is "exactly equal to" (value and type) x === 5
is true, x === "5"
is false
add a comment |
Given x = 5
1) Operator : == is "equal to". x == 8
is false
2) Operator : === is "exactly equal to" (value and type) x === 5
is true, x === "5"
is false
Given x = 5
1) Operator : == is "equal to". x == 8
is false
2) Operator : === is "exactly equal to" (value and type) x === 5
is true, x === "5"
is false
edited Aug 17 '09 at 13:38
nickf
374k173585687
374k173585687
answered Aug 17 '09 at 12:33
MannusanghiMannusanghi
149139
149139
add a comment |
add a comment |
Few of the examples
var_dump(5 == 5); // True
var_dump(5 == "5"); // True because == checks only same value not type
var_dump(5 === 5); // True
var_dump(5 === "5"); // False because value are same but data type are different.
P.S.
== Compares the value only, it won't bother about the data types
vs.
=== Compares the values and data types
what's the problem with this answer?
– Mohit Tanwani
Aug 27 '16 at 7:56
Why did people downvote this answer?
– John Demetriou
Oct 23 '16 at 8:25
add a comment |
Few of the examples
var_dump(5 == 5); // True
var_dump(5 == "5"); // True because == checks only same value not type
var_dump(5 === 5); // True
var_dump(5 === "5"); // False because value are same but data type are different.
P.S.
== Compares the value only, it won't bother about the data types
vs.
=== Compares the values and data types
what's the problem with this answer?
– Mohit Tanwani
Aug 27 '16 at 7:56
Why did people downvote this answer?
– John Demetriou
Oct 23 '16 at 8:25
add a comment |
Few of the examples
var_dump(5 == 5); // True
var_dump(5 == "5"); // True because == checks only same value not type
var_dump(5 === 5); // True
var_dump(5 === "5"); // False because value are same but data type are different.
P.S.
== Compares the value only, it won't bother about the data types
vs.
=== Compares the values and data types
Few of the examples
var_dump(5 == 5); // True
var_dump(5 == "5"); // True because == checks only same value not type
var_dump(5 === 5); // True
var_dump(5 === "5"); // False because value are same but data type are different.
P.S.
== Compares the value only, it won't bother about the data types
vs.
=== Compares the values and data types
edited Aug 26 '16 at 15:55
answered Aug 26 '16 at 15:48


Mohit TanwaniMohit Tanwani
6,0921828
6,0921828
what's the problem with this answer?
– Mohit Tanwani
Aug 27 '16 at 7:56
Why did people downvote this answer?
– John Demetriou
Oct 23 '16 at 8:25
add a comment |
what's the problem with this answer?
– Mohit Tanwani
Aug 27 '16 at 7:56
Why did people downvote this answer?
– John Demetriou
Oct 23 '16 at 8:25
what's the problem with this answer?
– Mohit Tanwani
Aug 27 '16 at 7:56
what's the problem with this answer?
– Mohit Tanwani
Aug 27 '16 at 7:56
Why did people downvote this answer?
– John Demetriou
Oct 23 '16 at 8:25
Why did people downvote this answer?
– John Demetriou
Oct 23 '16 at 8:25
add a comment |
$a = 5; // 5 as an integer
var_dump($a == 5); // compare value; return true
var_dump($a == '5'); // compare value (ignore type); return true
var_dump($a === 5); // compare type/value (integer vs. integer); return true
var_dump($a === '5'); // compare type/value (integer vs. string); return false
Be careful though. Here is a notorious problem.
// 'test' is found at position 0, which is interpreted as the boolean 'false'
if (strpos('testing', 'test')) {
// code...
}
vs.
// true, as strict comparison was made (0 !== false)
if (strpos('testing', 'test') !== false) {
// code...
}
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:51
add a comment |
$a = 5; // 5 as an integer
var_dump($a == 5); // compare value; return true
var_dump($a == '5'); // compare value (ignore type); return true
var_dump($a === 5); // compare type/value (integer vs. integer); return true
var_dump($a === '5'); // compare type/value (integer vs. string); return false
Be careful though. Here is a notorious problem.
// 'test' is found at position 0, which is interpreted as the boolean 'false'
if (strpos('testing', 'test')) {
// code...
}
vs.
// true, as strict comparison was made (0 !== false)
if (strpos('testing', 'test') !== false) {
// code...
}
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:51
add a comment |
$a = 5; // 5 as an integer
var_dump($a == 5); // compare value; return true
var_dump($a == '5'); // compare value (ignore type); return true
var_dump($a === 5); // compare type/value (integer vs. integer); return true
var_dump($a === '5'); // compare type/value (integer vs. string); return false
Be careful though. Here is a notorious problem.
// 'test' is found at position 0, which is interpreted as the boolean 'false'
if (strpos('testing', 'test')) {
// code...
}
vs.
// true, as strict comparison was made (0 !== false)
if (strpos('testing', 'test') !== false) {
// code...
}
$a = 5; // 5 as an integer
var_dump($a == 5); // compare value; return true
var_dump($a == '5'); // compare value (ignore type); return true
var_dump($a === 5); // compare type/value (integer vs. integer); return true
var_dump($a === '5'); // compare type/value (integer vs. string); return false
Be careful though. Here is a notorious problem.
// 'test' is found at position 0, which is interpreted as the boolean 'false'
if (strpos('testing', 'test')) {
// code...
}
vs.
// true, as strict comparison was made (0 !== false)
if (strpos('testing', 'test') !== false) {
// code...
}
answered Aug 1 '13 at 7:30
SephSeph
8471016
8471016
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:51
add a comment |
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:51
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:51
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:51
add a comment |
In short, === works in the same manner that == does in most other programming languages.
PHP allows you to make comparisons that don't really make sense. Example:
$y = "wauv";
$x = false;
if ($x == $y)
...
While this allows for some interesting "shortcuts" you should beware since a function that returns something it shouldn't (like "error" instead of a number) will not get caught, and you will be left wondering what happened.
In PHP, == compares values and performs type conversion if necessary (for instance, the string "12343sdfjskfjds" will become "12343" in an integer comparison). === will compare the value AND type and will return false if the type is not the same.
If you look in the PHP manual, you will see that a lot of functions return "false" if the function fails, but they might return 0 in a successful scenario, which is why they recommend doing "if (function() !== false)" to avoid mistakes.
1
It should be noted that in addition to those "shortcuts", the abnormal behavoir of the == operator has been known to open security holes, for example a popular PHP forum where it was possible to set the cookies password hash value to true, circumventing the if(databasehash==cookiehash) validation.
– David
Feb 26 '09 at 11:44
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:50
add a comment |
In short, === works in the same manner that == does in most other programming languages.
PHP allows you to make comparisons that don't really make sense. Example:
$y = "wauv";
$x = false;
if ($x == $y)
...
While this allows for some interesting "shortcuts" you should beware since a function that returns something it shouldn't (like "error" instead of a number) will not get caught, and you will be left wondering what happened.
In PHP, == compares values and performs type conversion if necessary (for instance, the string "12343sdfjskfjds" will become "12343" in an integer comparison). === will compare the value AND type and will return false if the type is not the same.
If you look in the PHP manual, you will see that a lot of functions return "false" if the function fails, but they might return 0 in a successful scenario, which is why they recommend doing "if (function() !== false)" to avoid mistakes.
1
It should be noted that in addition to those "shortcuts", the abnormal behavoir of the == operator has been known to open security holes, for example a popular PHP forum where it was possible to set the cookies password hash value to true, circumventing the if(databasehash==cookiehash) validation.
– David
Feb 26 '09 at 11:44
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:50
add a comment |
In short, === works in the same manner that == does in most other programming languages.
PHP allows you to make comparisons that don't really make sense. Example:
$y = "wauv";
$x = false;
if ($x == $y)
...
While this allows for some interesting "shortcuts" you should beware since a function that returns something it shouldn't (like "error" instead of a number) will not get caught, and you will be left wondering what happened.
In PHP, == compares values and performs type conversion if necessary (for instance, the string "12343sdfjskfjds" will become "12343" in an integer comparison). === will compare the value AND type and will return false if the type is not the same.
If you look in the PHP manual, you will see that a lot of functions return "false" if the function fails, but they might return 0 in a successful scenario, which is why they recommend doing "if (function() !== false)" to avoid mistakes.
In short, === works in the same manner that == does in most other programming languages.
PHP allows you to make comparisons that don't really make sense. Example:
$y = "wauv";
$x = false;
if ($x == $y)
...
While this allows for some interesting "shortcuts" you should beware since a function that returns something it shouldn't (like "error" instead of a number) will not get caught, and you will be left wondering what happened.
In PHP, == compares values and performs type conversion if necessary (for instance, the string "12343sdfjskfjds" will become "12343" in an integer comparison). === will compare the value AND type and will return false if the type is not the same.
If you look in the PHP manual, you will see that a lot of functions return "false" if the function fails, but they might return 0 in a successful scenario, which is why they recommend doing "if (function() !== false)" to avoid mistakes.
edited Jan 3 '16 at 15:47


Peter Mortensen
13.7k1986111
13.7k1986111
answered Feb 26 '09 at 11:27
Christian P.Christian P.
2,96764063
2,96764063
1
It should be noted that in addition to those "shortcuts", the abnormal behavoir of the == operator has been known to open security holes, for example a popular PHP forum where it was possible to set the cookies password hash value to true, circumventing the if(databasehash==cookiehash) validation.
– David
Feb 26 '09 at 11:44
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:50
add a comment |
1
It should be noted that in addition to those "shortcuts", the abnormal behavoir of the == operator has been known to open security holes, for example a popular PHP forum where it was possible to set the cookies password hash value to true, circumventing the if(databasehash==cookiehash) validation.
– David
Feb 26 '09 at 11:44
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:50
1
1
It should be noted that in addition to those "shortcuts", the abnormal behavoir of the == operator has been known to open security holes, for example a popular PHP forum where it was possible to set the cookies password hash value to true, circumventing the if(databasehash==cookiehash) validation.
– David
Feb 26 '09 at 11:44
It should be noted that in addition to those "shortcuts", the abnormal behavoir of the == operator has been known to open security holes, for example a popular PHP forum where it was possible to set the cookies password hash value to true, circumventing the if(databasehash==cookiehash) validation.
– David
Feb 26 '09 at 11:44
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:50
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:50
add a comment |
You would use === to test whether a function or variable is false rather than just equating to false (zero or an empty string).
$needle = 'a';
$haystack = 'abc';
$pos = strpos($haystack, $needle);
if ($pos === false) {
echo $needle . ' was not found in ' . $haystack;
} else {
echo $needle . ' was found in ' . $haystack . ' at location ' . $pos;
}
In this case strpos would return 0 which would equate to false in the test
if ($pos == false)
or
if (!$pos)
which is not what you want here.
add a comment |
You would use === to test whether a function or variable is false rather than just equating to false (zero or an empty string).
$needle = 'a';
$haystack = 'abc';
$pos = strpos($haystack, $needle);
if ($pos === false) {
echo $needle . ' was not found in ' . $haystack;
} else {
echo $needle . ' was found in ' . $haystack . ' at location ' . $pos;
}
In this case strpos would return 0 which would equate to false in the test
if ($pos == false)
or
if (!$pos)
which is not what you want here.
add a comment |
You would use === to test whether a function or variable is false rather than just equating to false (zero or an empty string).
$needle = 'a';
$haystack = 'abc';
$pos = strpos($haystack, $needle);
if ($pos === false) {
echo $needle . ' was not found in ' . $haystack;
} else {
echo $needle . ' was found in ' . $haystack . ' at location ' . $pos;
}
In this case strpos would return 0 which would equate to false in the test
if ($pos == false)
or
if (!$pos)
which is not what you want here.
You would use === to test whether a function or variable is false rather than just equating to false (zero or an empty string).
$needle = 'a';
$haystack = 'abc';
$pos = strpos($haystack, $needle);
if ($pos === false) {
echo $needle . ' was not found in ' . $haystack;
} else {
echo $needle . ' was found in ' . $haystack . ' at location ' . $pos;
}
In this case strpos would return 0 which would equate to false in the test
if ($pos == false)
or
if (!$pos)
which is not what you want here.
answered Sep 17 '08 at 7:07
Stacey RichardsStacey Richards
5,06873039
5,06873039
add a comment |
add a comment |
As for when to use one over the other, take for example the fwrite()
function in PHP.
This function writes content to a file stream. According to PHP, "fwrite()
returns the number of bytes written, or FALSE on error.". If you want to test if the function call was successful, this method is flawed:
if (!fwrite(stuff))
{
log('error!');
}
It can return zero (and is considered successful), and your condition still gets triggered. The right way would be:
if (fwrite(stuff) === FALSE)
{
log('error!');
}
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:50
add a comment |
As for when to use one over the other, take for example the fwrite()
function in PHP.
This function writes content to a file stream. According to PHP, "fwrite()
returns the number of bytes written, or FALSE on error.". If you want to test if the function call was successful, this method is flawed:
if (!fwrite(stuff))
{
log('error!');
}
It can return zero (and is considered successful), and your condition still gets triggered. The right way would be:
if (fwrite(stuff) === FALSE)
{
log('error!');
}
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:50
add a comment |
As for when to use one over the other, take for example the fwrite()
function in PHP.
This function writes content to a file stream. According to PHP, "fwrite()
returns the number of bytes written, or FALSE on error.". If you want to test if the function call was successful, this method is flawed:
if (!fwrite(stuff))
{
log('error!');
}
It can return zero (and is considered successful), and your condition still gets triggered. The right way would be:
if (fwrite(stuff) === FALSE)
{
log('error!');
}
As for when to use one over the other, take for example the fwrite()
function in PHP.
This function writes content to a file stream. According to PHP, "fwrite()
returns the number of bytes written, or FALSE on error.". If you want to test if the function call was successful, this method is flawed:
if (!fwrite(stuff))
{
log('error!');
}
It can return zero (and is considered successful), and your condition still gets triggered. The right way would be:
if (fwrite(stuff) === FALSE)
{
log('error!');
}
edited Jan 3 '16 at 15:45


Peter Mortensen
13.7k1986111
13.7k1986111
answered Feb 26 '09 at 11:25
MarioMario
1,5021010
1,5021010
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:50
add a comment |
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:50
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:50
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:50
add a comment |
PHP is a loosely typed language. Using the double equal operator allows for a loose checking of a variable.
Loosely checking a value would allow for some similar, but not equal, values to equate as the same:
- ''
- null
- false
- 0
All of these values would equate as equal using the double equal operator.
add a comment |
PHP is a loosely typed language. Using the double equal operator allows for a loose checking of a variable.
Loosely checking a value would allow for some similar, but not equal, values to equate as the same:
- ''
- null
- false
- 0
All of these values would equate as equal using the double equal operator.
add a comment |
PHP is a loosely typed language. Using the double equal operator allows for a loose checking of a variable.
Loosely checking a value would allow for some similar, but not equal, values to equate as the same:
- ''
- null
- false
- 0
All of these values would equate as equal using the double equal operator.
PHP is a loosely typed language. Using the double equal operator allows for a loose checking of a variable.
Loosely checking a value would allow for some similar, but not equal, values to equate as the same:
- ''
- null
- false
- 0
All of these values would equate as equal using the double equal operator.
answered Jan 27 '16 at 21:34
Cory CollierCory Collier
5131615
5131615
add a comment |
add a comment |
Variables have a type and a value.
- $var = "test" is a string that contain "test"
- $var2 = 24 is an integer vhose value is 24.
When you use these variables (in PHP), sometimes you don't have the good type.
For example, if you do
if ($var == 1) {... do something ...}
PHP have to convert ("to cast") $var to integer. In this case, "$var == 1" is true because any non-empty string is casted to 1.
When using ===, you check that the value AND THE TYPE are equal, so "$var === 1" is false.
This is useful, for example, when you have a function that can return false (on error) and 0 (result) :
if(myFunction() == false) { ... error on myFunction ... }
This code is wrong as if myFunction()
returns 0, it is casted to false and you seem to have an error. The correct code is :
if(myFunction() === false) { ... error on myFunction ... }
because the test is that the return value "is a boolean and is false" and not "can be casted to false".
regarding non-empty strings, that's actually not true. "a" == 0 is TRUE.
– nickf
Sep 17 '08 at 7:56
add a comment |
Variables have a type and a value.
- $var = "test" is a string that contain "test"
- $var2 = 24 is an integer vhose value is 24.
When you use these variables (in PHP), sometimes you don't have the good type.
For example, if you do
if ($var == 1) {... do something ...}
PHP have to convert ("to cast") $var to integer. In this case, "$var == 1" is true because any non-empty string is casted to 1.
When using ===, you check that the value AND THE TYPE are equal, so "$var === 1" is false.
This is useful, for example, when you have a function that can return false (on error) and 0 (result) :
if(myFunction() == false) { ... error on myFunction ... }
This code is wrong as if myFunction()
returns 0, it is casted to false and you seem to have an error. The correct code is :
if(myFunction() === false) { ... error on myFunction ... }
because the test is that the return value "is a boolean and is false" and not "can be casted to false".
regarding non-empty strings, that's actually not true. "a" == 0 is TRUE.
– nickf
Sep 17 '08 at 7:56
add a comment |
Variables have a type and a value.
- $var = "test" is a string that contain "test"
- $var2 = 24 is an integer vhose value is 24.
When you use these variables (in PHP), sometimes you don't have the good type.
For example, if you do
if ($var == 1) {... do something ...}
PHP have to convert ("to cast") $var to integer. In this case, "$var == 1" is true because any non-empty string is casted to 1.
When using ===, you check that the value AND THE TYPE are equal, so "$var === 1" is false.
This is useful, for example, when you have a function that can return false (on error) and 0 (result) :
if(myFunction() == false) { ... error on myFunction ... }
This code is wrong as if myFunction()
returns 0, it is casted to false and you seem to have an error. The correct code is :
if(myFunction() === false) { ... error on myFunction ... }
because the test is that the return value "is a boolean and is false" and not "can be casted to false".
Variables have a type and a value.
- $var = "test" is a string that contain "test"
- $var2 = 24 is an integer vhose value is 24.
When you use these variables (in PHP), sometimes you don't have the good type.
For example, if you do
if ($var == 1) {... do something ...}
PHP have to convert ("to cast") $var to integer. In this case, "$var == 1" is true because any non-empty string is casted to 1.
When using ===, you check that the value AND THE TYPE are equal, so "$var === 1" is false.
This is useful, for example, when you have a function that can return false (on error) and 0 (result) :
if(myFunction() == false) { ... error on myFunction ... }
This code is wrong as if myFunction()
returns 0, it is casted to false and you seem to have an error. The correct code is :
if(myFunction() === false) { ... error on myFunction ... }
because the test is that the return value "is a boolean and is false" and not "can be casted to false".
answered Sep 17 '08 at 7:42
ofauraxofaurax
67011221
67011221
regarding non-empty strings, that's actually not true. "a" == 0 is TRUE.
– nickf
Sep 17 '08 at 7:56
add a comment |
regarding non-empty strings, that's actually not true. "a" == 0 is TRUE.
– nickf
Sep 17 '08 at 7:56
regarding non-empty strings, that's actually not true. "a" == 0 is TRUE.
– nickf
Sep 17 '08 at 7:56
regarding non-empty strings, that's actually not true. "a" == 0 is TRUE.
– nickf
Sep 17 '08 at 7:56
add a comment |
The ===
operator is supposed to compare exact content equality while the ==
operator would compare semantic equality. In particular it will coerce strings to numbers.
Equality is a vast subject. See the Wikipedia article on equality.
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:51
add a comment |
The ===
operator is supposed to compare exact content equality while the ==
operator would compare semantic equality. In particular it will coerce strings to numbers.
Equality is a vast subject. See the Wikipedia article on equality.
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:51
add a comment |
The ===
operator is supposed to compare exact content equality while the ==
operator would compare semantic equality. In particular it will coerce strings to numbers.
Equality is a vast subject. See the Wikipedia article on equality.
The ===
operator is supposed to compare exact content equality while the ==
operator would compare semantic equality. In particular it will coerce strings to numbers.
Equality is a vast subject. See the Wikipedia article on equality.
edited Jan 3 '16 at 15:43


Peter Mortensen
13.7k1986111
13.7k1986111
answered Feb 26 '09 at 7:55
kmkaplankmkaplan
15.9k34459
15.9k34459
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:51
add a comment |
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:51
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:51
Merged from stackoverflow.com/questions/589549/php-vs-operator
– Shog9♦
Sep 18 '14 at 4:51
add a comment |
<?php
/**
* Comparison of two PHP objects == ===
* Checks for
* 1. References yes yes
* 2. Instances with matching attributes and its values yes no
* 3. Instances with different attributes yes no
**/
// There is no need to worry about comparing visibility of property or
// method, because it will be the same whenever an object instance is
// created, however visibility of an object can be modified during run
// time using ReflectionClass()
// http://php.net/manual/en/reflectionproperty.setaccessible.php
//
class Foo
{
public $foobar = 1;
public function createNewProperty($name, $value)
{
$this->{$name} = $value;
}
}
class Bar
{
}
// 1. Object handles or references
// Is an object a reference to itself or a clone or totally a different object?
//
// == true Name of two objects are same, for example, Foo() and Foo()
// == false Name of two objects are different, for example, Foo() and Bar()
// === true ID of two objects are same, for example, 1 and 1
// === false ID of two objects are different, for example, 1 and 2
echo "1. Object handles or references (both == and ===) <br />";
$bar = new Foo(); // New object Foo() created
$bar2 = new Foo(); // New object Foo() created
$baz = clone $bar; // Object Foo() cloned
$qux = $bar; // Object Foo() referenced
$norf = new Bar(); // New object Bar() created
echo "bar";
var_dump($bar);
echo "baz";
var_dump($baz);
echo "qux";
var_dump($qux);
echo "bar2";
var_dump($bar2);
echo "norf";
var_dump($norf);
// Clone: == true and === false
echo '$bar == $bar2';
var_dump($bar == $bar2); // true
echo '$bar === $bar2';
var_dump($bar === $bar2); // false
echo '$bar == $baz';
var_dump($bar == $baz); // true
echo '$bar === $baz';
var_dump($bar === $baz); // false
// Object reference: == true and === true
echo '$bar == $qux';
var_dump($bar == $qux); // true
echo '$bar === $qux';
var_dump($bar === $qux); // true
// Two different objects: == false and === false
echo '$bar == $norf';
var_dump($bar == $norf); // false
echo '$bar === $norf';
var_dump($bar === $norf); // false
// 2. Instances with matching attributes and its values (only ==).
// What happens when objects (even in cloned object) have same
// attributes but varying values?
// $foobar value is different
echo "2. Instances with matching attributes and its values (only ==) <br />";
$baz->foobar = 2;
echo '$foobar' . " value is different <br />";
echo '$bar->foobar = ' . $bar->foobar . "<br />";
echo '$baz->foobar = ' . $baz->foobar . "<br />";
echo '$bar == $baz';
var_dump($bar == $baz); // false
// $foobar's value is the same again
$baz->foobar = 1;
echo '$foobar' . " value is the same again <br />";
echo '$bar->foobar is ' . $bar->foobar . "<br />";
echo '$baz->foobar is ' . $baz->foobar . "<br />";
echo '$bar == $baz';
var_dump($bar == $baz); // true
// Changing values of properties in $qux object will change the property
// value of $bar and evaluates true always, because $qux = &$bar.
$qux->foobar = 2;
echo '$foobar value of both $qux and $bar is 2, because $qux = &$bar' . "<br />";
echo '$qux->foobar is ' . $qux->foobar . "<br />";
echo '$bar->foobar is ' . $bar->foobar . "<br />";
echo '$bar == $qux';
var_dump($bar == $qux); // true
// 3. Instances with different attributes (only ==)
// What happens when objects have different attributes even though
// one of the attributes has same value?
echo "3. Instances with different attributes (only ==) <br />";
// Dynamically create a property with the name in $name and value
// in $value for baz object
$name = 'newproperty';
$value = null;
$baz->createNewProperty($name, $value);
echo '$baz->newproperty is ' . $baz->{$name};
var_dump($baz);
$baz->foobar = 2;
echo '$foobar' . " value is same again <br />";
echo '$bar->foobar is ' . $bar->foobar . "<br />";
echo '$baz->foobar is ' . $baz->foobar . "<br />";
echo '$bar == $baz';
var_dump($bar == $baz); // false
var_dump($bar);
var_dump($baz);
?>
add a comment |
<?php
/**
* Comparison of two PHP objects == ===
* Checks for
* 1. References yes yes
* 2. Instances with matching attributes and its values yes no
* 3. Instances with different attributes yes no
**/
// There is no need to worry about comparing visibility of property or
// method, because it will be the same whenever an object instance is
// created, however visibility of an object can be modified during run
// time using ReflectionClass()
// http://php.net/manual/en/reflectionproperty.setaccessible.php
//
class Foo
{
public $foobar = 1;
public function createNewProperty($name, $value)
{
$this->{$name} = $value;
}
}
class Bar
{
}
// 1. Object handles or references
// Is an object a reference to itself or a clone or totally a different object?
//
// == true Name of two objects are same, for example, Foo() and Foo()
// == false Name of two objects are different, for example, Foo() and Bar()
// === true ID of two objects are same, for example, 1 and 1
// === false ID of two objects are different, for example, 1 and 2
echo "1. Object handles or references (both == and ===) <br />";
$bar = new Foo(); // New object Foo() created
$bar2 = new Foo(); // New object Foo() created
$baz = clone $bar; // Object Foo() cloned
$qux = $bar; // Object Foo() referenced
$norf = new Bar(); // New object Bar() created
echo "bar";
var_dump($bar);
echo "baz";
var_dump($baz);
echo "qux";
var_dump($qux);
echo "bar2";
var_dump($bar2);
echo "norf";
var_dump($norf);
// Clone: == true and === false
echo '$bar == $bar2';
var_dump($bar == $bar2); // true
echo '$bar === $bar2';
var_dump($bar === $bar2); // false
echo '$bar == $baz';
var_dump($bar == $baz); // true
echo '$bar === $baz';
var_dump($bar === $baz); // false
// Object reference: == true and === true
echo '$bar == $qux';
var_dump($bar == $qux); // true
echo '$bar === $qux';
var_dump($bar === $qux); // true
// Two different objects: == false and === false
echo '$bar == $norf';
var_dump($bar == $norf); // false
echo '$bar === $norf';
var_dump($bar === $norf); // false
// 2. Instances with matching attributes and its values (only ==).
// What happens when objects (even in cloned object) have same
// attributes but varying values?
// $foobar value is different
echo "2. Instances with matching attributes and its values (only ==) <br />";
$baz->foobar = 2;
echo '$foobar' . " value is different <br />";
echo '$bar->foobar = ' . $bar->foobar . "<br />";
echo '$baz->foobar = ' . $baz->foobar . "<br />";
echo '$bar == $baz';
var_dump($bar == $baz); // false
// $foobar's value is the same again
$baz->foobar = 1;
echo '$foobar' . " value is the same again <br />";
echo '$bar->foobar is ' . $bar->foobar . "<br />";
echo '$baz->foobar is ' . $baz->foobar . "<br />";
echo '$bar == $baz';
var_dump($bar == $baz); // true
// Changing values of properties in $qux object will change the property
// value of $bar and evaluates true always, because $qux = &$bar.
$qux->foobar = 2;
echo '$foobar value of both $qux and $bar is 2, because $qux = &$bar' . "<br />";
echo '$qux->foobar is ' . $qux->foobar . "<br />";
echo '$bar->foobar is ' . $bar->foobar . "<br />";
echo '$bar == $qux';
var_dump($bar == $qux); // true
// 3. Instances with different attributes (only ==)
// What happens when objects have different attributes even though
// one of the attributes has same value?
echo "3. Instances with different attributes (only ==) <br />";
// Dynamically create a property with the name in $name and value
// in $value for baz object
$name = 'newproperty';
$value = null;
$baz->createNewProperty($name, $value);
echo '$baz->newproperty is ' . $baz->{$name};
var_dump($baz);
$baz->foobar = 2;
echo '$foobar' . " value is same again <br />";
echo '$bar->foobar is ' . $bar->foobar . "<br />";
echo '$baz->foobar is ' . $baz->foobar . "<br />";
echo '$bar == $baz';
var_dump($bar == $baz); // false
var_dump($bar);
var_dump($baz);
?>
add a comment |
<?php
/**
* Comparison of two PHP objects == ===
* Checks for
* 1. References yes yes
* 2. Instances with matching attributes and its values yes no
* 3. Instances with different attributes yes no
**/
// There is no need to worry about comparing visibility of property or
// method, because it will be the same whenever an object instance is
// created, however visibility of an object can be modified during run
// time using ReflectionClass()
// http://php.net/manual/en/reflectionproperty.setaccessible.php
//
class Foo
{
public $foobar = 1;
public function createNewProperty($name, $value)
{
$this->{$name} = $value;
}
}
class Bar
{
}
// 1. Object handles or references
// Is an object a reference to itself or a clone or totally a different object?
//
// == true Name of two objects are same, for example, Foo() and Foo()
// == false Name of two objects are different, for example, Foo() and Bar()
// === true ID of two objects are same, for example, 1 and 1
// === false ID of two objects are different, for example, 1 and 2
echo "1. Object handles or references (both == and ===) <br />";
$bar = new Foo(); // New object Foo() created
$bar2 = new Foo(); // New object Foo() created
$baz = clone $bar; // Object Foo() cloned
$qux = $bar; // Object Foo() referenced
$norf = new Bar(); // New object Bar() created
echo "bar";
var_dump($bar);
echo "baz";
var_dump($baz);
echo "qux";
var_dump($qux);
echo "bar2";
var_dump($bar2);
echo "norf";
var_dump($norf);
// Clone: == true and === false
echo '$bar == $bar2';
var_dump($bar == $bar2); // true
echo '$bar === $bar2';
var_dump($bar === $bar2); // false
echo '$bar == $baz';
var_dump($bar == $baz); // true
echo '$bar === $baz';
var_dump($bar === $baz); // false
// Object reference: == true and === true
echo '$bar == $qux';
var_dump($bar == $qux); // true
echo '$bar === $qux';
var_dump($bar === $qux); // true
// Two different objects: == false and === false
echo '$bar == $norf';
var_dump($bar == $norf); // false
echo '$bar === $norf';
var_dump($bar === $norf); // false
// 2. Instances with matching attributes and its values (only ==).
// What happens when objects (even in cloned object) have same
// attributes but varying values?
// $foobar value is different
echo "2. Instances with matching attributes and its values (only ==) <br />";
$baz->foobar = 2;
echo '$foobar' . " value is different <br />";
echo '$bar->foobar = ' . $bar->foobar . "<br />";
echo '$baz->foobar = ' . $baz->foobar . "<br />";
echo '$bar == $baz';
var_dump($bar == $baz); // false
// $foobar's value is the same again
$baz->foobar = 1;
echo '$foobar' . " value is the same again <br />";
echo '$bar->foobar is ' . $bar->foobar . "<br />";
echo '$baz->foobar is ' . $baz->foobar . "<br />";
echo '$bar == $baz';
var_dump($bar == $baz); // true
// Changing values of properties in $qux object will change the property
// value of $bar and evaluates true always, because $qux = &$bar.
$qux->foobar = 2;
echo '$foobar value of both $qux and $bar is 2, because $qux = &$bar' . "<br />";
echo '$qux->foobar is ' . $qux->foobar . "<br />";
echo '$bar->foobar is ' . $bar->foobar . "<br />";
echo '$bar == $qux';
var_dump($bar == $qux); // true
// 3. Instances with different attributes (only ==)
// What happens when objects have different attributes even though
// one of the attributes has same value?
echo "3. Instances with different attributes (only ==) <br />";
// Dynamically create a property with the name in $name and value
// in $value for baz object
$name = 'newproperty';
$value = null;
$baz->createNewProperty($name, $value);
echo '$baz->newproperty is ' . $baz->{$name};
var_dump($baz);
$baz->foobar = 2;
echo '$foobar' . " value is same again <br />";
echo '$bar->foobar is ' . $bar->foobar . "<br />";
echo '$baz->foobar is ' . $baz->foobar . "<br />";
echo '$bar == $baz';
var_dump($bar == $baz); // false
var_dump($bar);
var_dump($baz);
?>
<?php
/**
* Comparison of two PHP objects == ===
* Checks for
* 1. References yes yes
* 2. Instances with matching attributes and its values yes no
* 3. Instances with different attributes yes no
**/
// There is no need to worry about comparing visibility of property or
// method, because it will be the same whenever an object instance is
// created, however visibility of an object can be modified during run
// time using ReflectionClass()
// http://php.net/manual/en/reflectionproperty.setaccessible.php
//
class Foo
{
public $foobar = 1;
public function createNewProperty($name, $value)
{
$this->{$name} = $value;
}
}
class Bar
{
}
// 1. Object handles or references
// Is an object a reference to itself or a clone or totally a different object?
//
// == true Name of two objects are same, for example, Foo() and Foo()
// == false Name of two objects are different, for example, Foo() and Bar()
// === true ID of two objects are same, for example, 1 and 1
// === false ID of two objects are different, for example, 1 and 2
echo "1. Object handles or references (both == and ===) <br />";
$bar = new Foo(); // New object Foo() created
$bar2 = new Foo(); // New object Foo() created
$baz = clone $bar; // Object Foo() cloned
$qux = $bar; // Object Foo() referenced
$norf = new Bar(); // New object Bar() created
echo "bar";
var_dump($bar);
echo "baz";
var_dump($baz);
echo "qux";
var_dump($qux);
echo "bar2";
var_dump($bar2);
echo "norf";
var_dump($norf);
// Clone: == true and === false
echo '$bar == $bar2';
var_dump($bar == $bar2); // true
echo '$bar === $bar2';
var_dump($bar === $bar2); // false
echo '$bar == $baz';
var_dump($bar == $baz); // true
echo '$bar === $baz';
var_dump($bar === $baz); // false
// Object reference: == true and === true
echo '$bar == $qux';
var_dump($bar == $qux); // true
echo '$bar === $qux';
var_dump($bar === $qux); // true
// Two different objects: == false and === false
echo '$bar == $norf';
var_dump($bar == $norf); // false
echo '$bar === $norf';
var_dump($bar === $norf); // false
// 2. Instances with matching attributes and its values (only ==).
// What happens when objects (even in cloned object) have same
// attributes but varying values?
// $foobar value is different
echo "2. Instances with matching attributes and its values (only ==) <br />";
$baz->foobar = 2;
echo '$foobar' . " value is different <br />";
echo '$bar->foobar = ' . $bar->foobar . "<br />";
echo '$baz->foobar = ' . $baz->foobar . "<br />";
echo '$bar == $baz';
var_dump($bar == $baz); // false
// $foobar's value is the same again
$baz->foobar = 1;
echo '$foobar' . " value is the same again <br />";
echo '$bar->foobar is ' . $bar->foobar . "<br />";
echo '$baz->foobar is ' . $baz->foobar . "<br />";
echo '$bar == $baz';
var_dump($bar == $baz); // true
// Changing values of properties in $qux object will change the property
// value of $bar and evaluates true always, because $qux = &$bar.
$qux->foobar = 2;
echo '$foobar value of both $qux and $bar is 2, because $qux = &$bar' . "<br />";
echo '$qux->foobar is ' . $qux->foobar . "<br />";
echo '$bar->foobar is ' . $bar->foobar . "<br />";
echo '$bar == $qux';
var_dump($bar == $qux); // true
// 3. Instances with different attributes (only ==)
// What happens when objects have different attributes even though
// one of the attributes has same value?
echo "3. Instances with different attributes (only ==) <br />";
// Dynamically create a property with the name in $name and value
// in $value for baz object
$name = 'newproperty';
$value = null;
$baz->createNewProperty($name, $value);
echo '$baz->newproperty is ' . $baz->{$name};
var_dump($baz);
$baz->foobar = 2;
echo '$foobar' . " value is same again <br />";
echo '$bar->foobar is ' . $bar->foobar . "<br />";
echo '$baz->foobar is ' . $baz->foobar . "<br />";
echo '$bar == $baz';
var_dump($bar == $baz); // false
var_dump($bar);
var_dump($baz);
?>
edited Jan 3 '16 at 15:57


Peter Mortensen
13.7k1986111
13.7k1986111
answered Sep 18 '15 at 0:51
SathishSathish
8,19122246
8,19122246
add a comment |
add a comment |
All of the answers so far ignore a dangerous problem with ===. It has been noted in passing, but not stressed, that integer and double are different types, so the following code:
$n = 1000;
$d = $n + 0.0e0;
echo '<br/>'. ( ($n == $d)?'equal' :'not equal' );
echo '<br/>'. ( ($n === $d)?'equal' :'not equal' );
gives:
equal
not equal
Note that this is NOT a case of a "rounding error". The two numbers are exactly equal down to the last bit, but they have different types.
This is a nasty problem because a program using === can run happily for years if all of the numbers are small enough (where "small enough" depends on the hardware and OS you are running on). However, if by chance, an integer happens to be large enough to be converted to a double, its type is changed "forever" even though a subsequent operation, or many operations, might bring it back to a small integer in value. And, it gets worse. It can spread - double-ness infection can be passed along to anything it touches, one calculation at a time.
In the real world, this is likely to be a problem in programs that handle dates beyond the year 2038, for example. At this time, UNIX timestamps (number of seconds since 1970-01-01 00:00:00 UTC) will require more than 32-bits, so their representation will "magically" switch to double on some systems. Therefore, if you calculate the difference between two times you might end up with a couple of seconds, but as a double, rather than the integer result that occurs in the year 2017.
I think this is much worse than conversions between strings and numbers because it is subtle. I find it easy to keep track of what is a string and what is a number, but keeping track of the number of bits in a number is beyond me.
So, in the above answers there are some nice tables, but no distinction between 1 (as an integer) and 1 (subtle double) and 1.0 (obvious double). Also, advice that you should always use === and never == is not great because === will sometimes fail where == works properly. Also, JavaScript is not equivalent in this regard because it has only one number type (internally it may have different bit-wise representations, but it does not cause problems for ===).
My advice - use neither. You need to write your own comparison function to really fix this mess.
add a comment |
All of the answers so far ignore a dangerous problem with ===. It has been noted in passing, but not stressed, that integer and double are different types, so the following code:
$n = 1000;
$d = $n + 0.0e0;
echo '<br/>'. ( ($n == $d)?'equal' :'not equal' );
echo '<br/>'. ( ($n === $d)?'equal' :'not equal' );
gives:
equal
not equal
Note that this is NOT a case of a "rounding error". The two numbers are exactly equal down to the last bit, but they have different types.
This is a nasty problem because a program using === can run happily for years if all of the numbers are small enough (where "small enough" depends on the hardware and OS you are running on). However, if by chance, an integer happens to be large enough to be converted to a double, its type is changed "forever" even though a subsequent operation, or many operations, might bring it back to a small integer in value. And, it gets worse. It can spread - double-ness infection can be passed along to anything it touches, one calculation at a time.
In the real world, this is likely to be a problem in programs that handle dates beyond the year 2038, for example. At this time, UNIX timestamps (number of seconds since 1970-01-01 00:00:00 UTC) will require more than 32-bits, so their representation will "magically" switch to double on some systems. Therefore, if you calculate the difference between two times you might end up with a couple of seconds, but as a double, rather than the integer result that occurs in the year 2017.
I think this is much worse than conversions between strings and numbers because it is subtle. I find it easy to keep track of what is a string and what is a number, but keeping track of the number of bits in a number is beyond me.
So, in the above answers there are some nice tables, but no distinction between 1 (as an integer) and 1 (subtle double) and 1.0 (obvious double). Also, advice that you should always use === and never == is not great because === will sometimes fail where == works properly. Also, JavaScript is not equivalent in this regard because it has only one number type (internally it may have different bit-wise representations, but it does not cause problems for ===).
My advice - use neither. You need to write your own comparison function to really fix this mess.
add a comment |
All of the answers so far ignore a dangerous problem with ===. It has been noted in passing, but not stressed, that integer and double are different types, so the following code:
$n = 1000;
$d = $n + 0.0e0;
echo '<br/>'. ( ($n == $d)?'equal' :'not equal' );
echo '<br/>'. ( ($n === $d)?'equal' :'not equal' );
gives:
equal
not equal
Note that this is NOT a case of a "rounding error". The two numbers are exactly equal down to the last bit, but they have different types.
This is a nasty problem because a program using === can run happily for years if all of the numbers are small enough (where "small enough" depends on the hardware and OS you are running on). However, if by chance, an integer happens to be large enough to be converted to a double, its type is changed "forever" even though a subsequent operation, or many operations, might bring it back to a small integer in value. And, it gets worse. It can spread - double-ness infection can be passed along to anything it touches, one calculation at a time.
In the real world, this is likely to be a problem in programs that handle dates beyond the year 2038, for example. At this time, UNIX timestamps (number of seconds since 1970-01-01 00:00:00 UTC) will require more than 32-bits, so their representation will "magically" switch to double on some systems. Therefore, if you calculate the difference between two times you might end up with a couple of seconds, but as a double, rather than the integer result that occurs in the year 2017.
I think this is much worse than conversions between strings and numbers because it is subtle. I find it easy to keep track of what is a string and what is a number, but keeping track of the number of bits in a number is beyond me.
So, in the above answers there are some nice tables, but no distinction between 1 (as an integer) and 1 (subtle double) and 1.0 (obvious double). Also, advice that you should always use === and never == is not great because === will sometimes fail where == works properly. Also, JavaScript is not equivalent in this regard because it has only one number type (internally it may have different bit-wise representations, but it does not cause problems for ===).
My advice - use neither. You need to write your own comparison function to really fix this mess.
All of the answers so far ignore a dangerous problem with ===. It has been noted in passing, but not stressed, that integer and double are different types, so the following code:
$n = 1000;
$d = $n + 0.0e0;
echo '<br/>'. ( ($n == $d)?'equal' :'not equal' );
echo '<br/>'. ( ($n === $d)?'equal' :'not equal' );
gives:
equal
not equal
Note that this is NOT a case of a "rounding error". The two numbers are exactly equal down to the last bit, but they have different types.
This is a nasty problem because a program using === can run happily for years if all of the numbers are small enough (where "small enough" depends on the hardware and OS you are running on). However, if by chance, an integer happens to be large enough to be converted to a double, its type is changed "forever" even though a subsequent operation, or many operations, might bring it back to a small integer in value. And, it gets worse. It can spread - double-ness infection can be passed along to anything it touches, one calculation at a time.
In the real world, this is likely to be a problem in programs that handle dates beyond the year 2038, for example. At this time, UNIX timestamps (number of seconds since 1970-01-01 00:00:00 UTC) will require more than 32-bits, so their representation will "magically" switch to double on some systems. Therefore, if you calculate the difference between two times you might end up with a couple of seconds, but as a double, rather than the integer result that occurs in the year 2017.
I think this is much worse than conversions between strings and numbers because it is subtle. I find it easy to keep track of what is a string and what is a number, but keeping track of the number of bits in a number is beyond me.
So, in the above answers there are some nice tables, but no distinction between 1 (as an integer) and 1 (subtle double) and 1.0 (obvious double). Also, advice that you should always use === and never == is not great because === will sometimes fail where == works properly. Also, JavaScript is not equivalent in this regard because it has only one number type (internally it may have different bit-wise representations, but it does not cause problems for ===).
My advice - use neither. You need to write your own comparison function to really fix this mess.
edited May 9 '17 at 16:31
answered May 8 '17 at 22:05
DavidWalleyDavidWalley
11411
11411
add a comment |
add a comment |
There are two differences between ==
and ===
in PHP arrays and objects that I think didn't mention here; two arrays with different key sorts, and objects.
Two arrays with different key sorts
If you have an array with a key sort and another array with a different key sort, they are strictly different (i.e. using ===
). That may cause if you key-sort an array, and try to compare the sorted array with the original one.
For instance, consider an empty array. First, we try to push some new indexes to the array without any special sort. A good example would be an array with strings as keys. Now deep into an example:
// Define an array
$arr = ;
// Adding unsorted keys
$arr["I"] = "we";
$arr["you"] = "you";
$arr["he"] = "they";
Now, we have a not-sorted-keys array (e.g., 'he' came after 'you'). Consider the same array, but we sorted its keys alphabetically:
// Declare array
$alphabetArr = ;
// Adding alphabetical-sorted keys
$alphabetArr["I"] = "we";
$alphabetArr["he"] = "they";
$alphabetArr["you"] = "you";
Tip: You can sort an array by key using ksort() function.
Now you have another array with a different key sort from the first one. So, we're going to compare them:
$arr == $alphabetArr; // true
$arr === $alphabetArr; // false
Note: It may be obvious, but comparing two different arrays using strict comparison always results false
. However, two arbitrary arrays may be equal using ===
or not.
You would say: "This difference is negligible". Then I say it's a difference and should be considered and may happen anytime. As mentioned above, sorting keys in an array is a good example of that.
Objects
Keep in mind, two different objects are never strict-equal. These examples would help:
$stdClass1 = new stdClass();
$stdClass2 = new stdClass();
$clonedStdClass1 = clone $stdClass1;
// Comparing
$stdClass1 == $stdClass2; // true
$stdClass1 === $stdClass2; // false
$stdClass1 == $clonedStdClass1; // true
$stdClass1 === $clonedStdClass1; // false
Note: Assigning an object to another variable does not create a copy - rather, it creates a reference to the same memory location as the object. See here.
Note: As of PHP7, anonymous classes was added. From the results, there is no difference between new class {}
and new stdClass()
in the tests above.
add a comment |
There are two differences between ==
and ===
in PHP arrays and objects that I think didn't mention here; two arrays with different key sorts, and objects.
Two arrays with different key sorts
If you have an array with a key sort and another array with a different key sort, they are strictly different (i.e. using ===
). That may cause if you key-sort an array, and try to compare the sorted array with the original one.
For instance, consider an empty array. First, we try to push some new indexes to the array without any special sort. A good example would be an array with strings as keys. Now deep into an example:
// Define an array
$arr = ;
// Adding unsorted keys
$arr["I"] = "we";
$arr["you"] = "you";
$arr["he"] = "they";
Now, we have a not-sorted-keys array (e.g., 'he' came after 'you'). Consider the same array, but we sorted its keys alphabetically:
// Declare array
$alphabetArr = ;
// Adding alphabetical-sorted keys
$alphabetArr["I"] = "we";
$alphabetArr["he"] = "they";
$alphabetArr["you"] = "you";
Tip: You can sort an array by key using ksort() function.
Now you have another array with a different key sort from the first one. So, we're going to compare them:
$arr == $alphabetArr; // true
$arr === $alphabetArr; // false
Note: It may be obvious, but comparing two different arrays using strict comparison always results false
. However, two arbitrary arrays may be equal using ===
or not.
You would say: "This difference is negligible". Then I say it's a difference and should be considered and may happen anytime. As mentioned above, sorting keys in an array is a good example of that.
Objects
Keep in mind, two different objects are never strict-equal. These examples would help:
$stdClass1 = new stdClass();
$stdClass2 = new stdClass();
$clonedStdClass1 = clone $stdClass1;
// Comparing
$stdClass1 == $stdClass2; // true
$stdClass1 === $stdClass2; // false
$stdClass1 == $clonedStdClass1; // true
$stdClass1 === $clonedStdClass1; // false
Note: Assigning an object to another variable does not create a copy - rather, it creates a reference to the same memory location as the object. See here.
Note: As of PHP7, anonymous classes was added. From the results, there is no difference between new class {}
and new stdClass()
in the tests above.
add a comment |
There are two differences between ==
and ===
in PHP arrays and objects that I think didn't mention here; two arrays with different key sorts, and objects.
Two arrays with different key sorts
If you have an array with a key sort and another array with a different key sort, they are strictly different (i.e. using ===
). That may cause if you key-sort an array, and try to compare the sorted array with the original one.
For instance, consider an empty array. First, we try to push some new indexes to the array without any special sort. A good example would be an array with strings as keys. Now deep into an example:
// Define an array
$arr = ;
// Adding unsorted keys
$arr["I"] = "we";
$arr["you"] = "you";
$arr["he"] = "they";
Now, we have a not-sorted-keys array (e.g., 'he' came after 'you'). Consider the same array, but we sorted its keys alphabetically:
// Declare array
$alphabetArr = ;
// Adding alphabetical-sorted keys
$alphabetArr["I"] = "we";
$alphabetArr["he"] = "they";
$alphabetArr["you"] = "you";
Tip: You can sort an array by key using ksort() function.
Now you have another array with a different key sort from the first one. So, we're going to compare them:
$arr == $alphabetArr; // true
$arr === $alphabetArr; // false
Note: It may be obvious, but comparing two different arrays using strict comparison always results false
. However, two arbitrary arrays may be equal using ===
or not.
You would say: "This difference is negligible". Then I say it's a difference and should be considered and may happen anytime. As mentioned above, sorting keys in an array is a good example of that.
Objects
Keep in mind, two different objects are never strict-equal. These examples would help:
$stdClass1 = new stdClass();
$stdClass2 = new stdClass();
$clonedStdClass1 = clone $stdClass1;
// Comparing
$stdClass1 == $stdClass2; // true
$stdClass1 === $stdClass2; // false
$stdClass1 == $clonedStdClass1; // true
$stdClass1 === $clonedStdClass1; // false
Note: Assigning an object to another variable does not create a copy - rather, it creates a reference to the same memory location as the object. See here.
Note: As of PHP7, anonymous classes was added. From the results, there is no difference between new class {}
and new stdClass()
in the tests above.
There are two differences between ==
and ===
in PHP arrays and objects that I think didn't mention here; two arrays with different key sorts, and objects.
Two arrays with different key sorts
If you have an array with a key sort and another array with a different key sort, they are strictly different (i.e. using ===
). That may cause if you key-sort an array, and try to compare the sorted array with the original one.
For instance, consider an empty array. First, we try to push some new indexes to the array without any special sort. A good example would be an array with strings as keys. Now deep into an example:
// Define an array
$arr = ;
// Adding unsorted keys
$arr["I"] = "we";
$arr["you"] = "you";
$arr["he"] = "they";
Now, we have a not-sorted-keys array (e.g., 'he' came after 'you'). Consider the same array, but we sorted its keys alphabetically:
// Declare array
$alphabetArr = ;
// Adding alphabetical-sorted keys
$alphabetArr["I"] = "we";
$alphabetArr["he"] = "they";
$alphabetArr["you"] = "you";
Tip: You can sort an array by key using ksort() function.
Now you have another array with a different key sort from the first one. So, we're going to compare them:
$arr == $alphabetArr; // true
$arr === $alphabetArr; // false
Note: It may be obvious, but comparing two different arrays using strict comparison always results false
. However, two arbitrary arrays may be equal using ===
or not.
You would say: "This difference is negligible". Then I say it's a difference and should be considered and may happen anytime. As mentioned above, sorting keys in an array is a good example of that.
Objects
Keep in mind, two different objects are never strict-equal. These examples would help:
$stdClass1 = new stdClass();
$stdClass2 = new stdClass();
$clonedStdClass1 = clone $stdClass1;
// Comparing
$stdClass1 == $stdClass2; // true
$stdClass1 === $stdClass2; // false
$stdClass1 == $clonedStdClass1; // true
$stdClass1 === $clonedStdClass1; // false
Note: Assigning an object to another variable does not create a copy - rather, it creates a reference to the same memory location as the object. See here.
Note: As of PHP7, anonymous classes was added. From the results, there is no difference between new class {}
and new stdClass()
in the tests above.
answered Sep 12 '18 at 16:15


MAChitgarhaMAChitgarha
81911020
81911020
add a comment |
add a comment |
php == is a comparison operator which compares the value of the variables. But === compares the value and the data type.
For example,
<?php
$var1 = 10;
$var2 = '10';
if($var1 == $var2) {
echo 'Variables are equal';
} else {
echo 'Variables are not equal';
}
?>
In this case the output will be 'Variables are equal', even though their data types are different.
But if we use === instead of ==, the output will be 'Variables are not equal'. The php first compares the value of the variable and then the data type. Here the values are same, but data types are different.
add a comment |
php == is a comparison operator which compares the value of the variables. But === compares the value and the data type.
For example,
<?php
$var1 = 10;
$var2 = '10';
if($var1 == $var2) {
echo 'Variables are equal';
} else {
echo 'Variables are not equal';
}
?>
In this case the output will be 'Variables are equal', even though their data types are different.
But if we use === instead of ==, the output will be 'Variables are not equal'. The php first compares the value of the variable and then the data type. Here the values are same, but data types are different.
add a comment |
php == is a comparison operator which compares the value of the variables. But === compares the value and the data type.
For example,
<?php
$var1 = 10;
$var2 = '10';
if($var1 == $var2) {
echo 'Variables are equal';
} else {
echo 'Variables are not equal';
}
?>
In this case the output will be 'Variables are equal', even though their data types are different.
But if we use === instead of ==, the output will be 'Variables are not equal'. The php first compares the value of the variable and then the data type. Here the values are same, but data types are different.
php == is a comparison operator which compares the value of the variables. But === compares the value and the data type.
For example,
<?php
$var1 = 10;
$var2 = '10';
if($var1 == $var2) {
echo 'Variables are equal';
} else {
echo 'Variables are not equal';
}
?>
In this case the output will be 'Variables are equal', even though their data types are different.
But if we use === instead of ==, the output will be 'Variables are not equal'. The php first compares the value of the variable and then the data type. Here the values are same, but data types are different.
answered Feb 4 at 2:04


2rahulsk2rahulsk
115
115
add a comment |
add a comment |
protected by Shankar Damodaran Feb 22 '14 at 9:24
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
2
Here is detailed comparison of both operator in JavaScript with performance test
– Zaheer Ahmed
Jan 8 '14 at 20:08