=== & !==

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

=== & !==

Post by bokehman »

What are === & !== used for?
wyred
Forum Commoner
Posts: 86
Joined: Mon Dec 20, 2004 1:59 am
Location: Singapore

Post 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";
}
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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 }
Post Reply