Page 1 of 1

=== & !==

Posted: Wed May 11, 2005 2:37 am
by bokehman
What are === & !== used for?

Posted: Wed May 11, 2005 2:40 am
by wyred
Besides comparing variable values, it compares the variable types too.

Code: Select all

$int = 123;
$string = '123';

if ($int === $string) {
  echo "something's wrong";
} else {
  echo "nothing wrong";
}

if ($int == $string) {
  echo "nothing wrong";
} else {
  echo "something's wrong";
}

Posted: Wed May 11, 2005 8:56 am
by Chris Corbyn
They are comparison and bitwise operators...

Read the manual at http://www.php.net/ for more insight but basically they mean:

Code: Select all

//Comparisons
==           Is the same as
!=           Not the same as
===          Identical to
!==          Not identical to

//Bitwise
&&           AND
||           OR

Posted: Wed May 11, 2005 10:30 am
by timvw
a more real life example :)

Code: Select all

function dostuff()
{
  // returns false if stuff failed
  // otherwise returns the number of processed items
}
in the case the operation succeeds and 0 items are processed, the function will return 0. However if you write fe:

Code: Select all

if (!dostuff()) { // operation was not a success }
The 0 will be evaluated to false.. And thus the code in the brackets will be executed... Therefore you have to test explicitely

Code: Select all

if (dostuff() !== false) { // operation was succes }