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.

Link to comment
Share on other sites

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.

 

 

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.