Jump to content

Whats the difference between !== and !=


jwhite68

Recommended Posts

Hi

!== and != is the converse of === and ==

So I give you an example:

You got two variables

One with the value true and one with the value 1:

Now the first request with !=

<?php
   $var1 = true;
   $var2 = 1;

   if ($var1 != true)
      echo 'The value of $var1 is false';
   else
      echo 'The value of $var1 is true';

   if ($var2 != true)
      echo 'The value of $var2 is false';
   else
      echo 'The value of $var2 is true';
?>

Now the same with !==

<?php
   $var1 = true;
   $var2 = 1;

   if ($var1 !== true)
      echo 'The value of $var1 is false';
   else
      echo 'The value of $var1 is true';

   if ($var2 !== true)
      echo 'The value of $var2 is false';
   else
      echo 'The value of $var2 is true';
?>

See the different?

 

So != controlls if the two values are the same but don't look after type of the two values.

!== controlls both... type of the variable and the values itself.

Hmm. I think I understand.

 

In my case, I was testing whether a variable (that could be assigned to a date field from a $_REQUEST['date']) was a null string or not.

 

So I am trying to understand whether its best practise to always use != for such scenarios.

Since !== doesnt work in this comparison.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.