This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Functions, Part I Forum

View Course » View Exercise

149 points
39cb2f09c0bd942ea4b0eced251148cf?s=140&d=retro
Submitted by
Bittermind
over 3 years ago

What is the difference between == and ===

This one works:

$myname = "Mike";
$search
char = "k";
if (strpos($myname, $searchchar) == true) {
echo "We found character " . "\"$searchchar\"" . " in " . $myname;
}
else {
echo "Sorry, we couldn't find any " . " \"$searchchar\"" . " in " . $myname;
}

But if you replace == to === it will not work properly

ex: if (strpos($myname, $searchchar) == true)

So what is the main difference?


5 votes

permalink

It seem I found it myself, please correct if I'm wrong.

From php.net
$a == $b must be equal, TRUE if $a is equal to $b after type juggling.
$a === $b must be identical, TRUE if $a is equal to $b, and they are of the same type.

And strpos can returns only FALSE if the needle was not found.
http://us3.php.net/manual/en/function.strpos.php

so strpos can work only with FALSE while comparing it with ===

149 points
39cb2f09c0bd942ea4b0eced251148cf?s=140&d=retro
Submitted by
Bittermind
over 3 years ago