Can someone help me understand these operators.
Thanks.
Several replies here distilled the core idea; and gave the short version and corrected 's confusing example. The concise takeaway: loose equality lets the language coerce types to try to match values, while strict equality requires type and value to match. That rule applies in both PHP and JavaScript, but the coercion rules differ, so examples that "look the same" can behave very differently across languages.
PHP notes and gotchas — loose comparisons can produce surprising matches because non-numeric strings and booleans are converted during comparison. Common defensive measures are using === when type matters, explicitly casting ((int), (bool), strval()), and using strict mode where available (for example in_array($needle, $haystack, true)). Examples:
<?php
var_dump('0' == false); // true
var_dump('0' === false); // false
var_dump('foo' == 0); // true — 'foo' converts to 0 in numeric context JavaScript notes — similar operator names but different rules. == follows a set of coercion steps (so null == undefined is true, 0 == false is true), while === is identity. NaN and -0 are special: NaN === NaN is false, Object.is can be used to distinguish NaN and -0. Examples:
console.log(null == undefined); // true
console.log(null === undefined); // false
console.log(0 == false); // true
console.log(0 === false); // false
console.log(NaN === NaN); // false
console.log(Object.is(NaN, NaN)); // true Practical guidance synthesizing thread contributions: prefer strict equality (===) unless a specific loose-coercion behavior is required and documented; when accepting external input, normalize types first; use language tools (var_dump, typeof, Object.is) to inspect values during debugging. For JavaScript equality details, see the MDN summary: MDN: Equality comparisons and sameness.
Jump to Post— mschroeder 251http://www.php.net/manual/en/language.operators.comparison.php
Manual page breaks down just about anything you could possibly want to know.
Jump to Post— mschroeder 251That is an incorrect example
<?php $a = 10; $b = 5+5; $c = '5+5'; var_dump($a); var_dump($b); var_dump($c); var_dump($a == $b); var_dump($a === $b); var_dump($b === $c);int 10 int 10 string '5+5' (length=3) boolean true boolean true boolean false
http://www.php.net/manual/en/language.operators.comparison.php
Manual page breaks down just about anything you could possibly want to know.
$i = 10;
$t = 5+5;
$i == $t TRUE
$i === $t FALSE
That is an incorrect example
<?php
$a = 10;
$b = 5+5;
$c = '5+5';
var_dump($a);
var_dump($b);
var_dump($c);
var_dump($a == $b);
var_dump($a === $b);
var_dump($b === $c); int 10
int 10
string '5+5' (length=3)
boolean true
boolean true
boolean false You can get the explanation with examples at the following link
in short what all others are trying to explain you is '==' only checks the contents of both the operands for equality and '===' compares for types (of both operands) too.
The difference between both of them is that :
"==" -> It only checks for equality without checking the datatype
"===" -> It checks for equality and also both the variables should be of same datatype
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.