Jump to content

simple question needs an answer


gibigbig

Recommended Posts

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 '=' ?

 

Link to comment
Share on other sites

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. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.