gibigbig Posted April 4, 2011 Share Posted April 4, 2011 i saw this in a php function page here: http://php.net/manual/en/function.stristr.php the line is if(stristr($string, 'earth') === FALSE) { specifically this this code <?php $string = 'Hello World!'; if(stristr($string, 'earth') === FALSE) { echo '"earth" not found in string'; } // outputs: "earth" not found in string ?> what is the significance of the '===' in there does it mean something? is there any difference from the '==' or the '=' ? Quote Link to comment https://forums.phpfreaks.com/topic/232629-simple-question-needs-an-answer/ Share on other sites More sharing options...
JasonLewis Posted April 4, 2011 Share Posted April 4, 2011 Quick overview: = is an assignment operator. You use it to assign values to variables, as you may have experienced. == is a comparison operator. Checks if variable is EQUAL TO another variable. === is a comparison operator. Checks if variable is IDENTICAL to another variable. So the main difference between the last to is that one checks the type as well, such as boolean or string etc. Example: $var1 = "true"; $var2 = true; if($var1 == $var2){ echo "var1 is equal to var2"; } if($var1 === $var2){ echo "This will not be echo'd, because var1 is not identical to var2"; } As you can see, $var1 is a string and $var2 is a boolean. So they are not identical. Hope that makes sense, and for further reference you can check out Comparisons at the manual. EDIT: Just a bit more of an explanation. The stripos() function isn't the best example of when you should use type comparison as well. Check out strpos(). Say you want to find the occurance of something in a string, and it's position is at the start. strpos() would return 0, because it's the first character. If you were to check if strpos() returned false, 0 would match this and your script would essentially think that it did not find a match when it in fact did. Here's an example: $string = "Hello World"; if(strpos($string, "Hello") == false){ echo 'Did not find "Hello"'; // This will be echo'd because "Hello" is at the start of the string (0) and 0 is equal to false. Bummer. } To overcome this problem, we need to check that the returned value of strpos() is identical to false, that is, the returned value is false itself, and not 0. $string = "Hello World"; if(strpos($string, "Hello") === false){ echo 'Did not find "Hello"'; // Now this won't be echo'd, because we are checking if they are identical. } Good luck. Quote Link to comment https://forums.phpfreaks.com/topic/232629-simple-question-needs-an-answer/#findComment-1196508 Share on other sites More sharing options...
lastkarrde Posted April 4, 2011 Share Posted April 4, 2011 = , assignment == , same value ===, same value and type Quote Link to comment https://forums.phpfreaks.com/topic/232629-simple-question-needs-an-answer/#findComment-1196511 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.