Equal Sign in PHP: Equality and Not Equals

The PHP equal sign can be used to assign the value of variable as well as evaluate a variable as part of an if-else statement or other conditional statement. However, there are subtle differences that are important for ensuring your PHP code is as accurate as possible. The following samples are from page 22.

The Equal Sign

Assignment ( = ): Assigns the value on the right to the variable on the left
Equality ( == ): Checks if the left and right values are equal
Identical ( === ): Checks if the left and right values are equal AND identical (same variable type)


$a = 1; // Sets the value of $a as the integer 1
$b = TRUE; // Sets the value of $b to the boolean TRUE
if ($a == $b){
echo 'a is equal to b.';
if ($a === $b){ // compare VALUE and data type: if $a(integer) === $b(boolean)
echo 'a is identical and equal to b.';
a is equal to b. 

Not ( ! ), Not Equal to ( != ), Not Identical to ( !== )

Used in conditional statements to evaluate as true a FALSE result of an expression or if a value is NOT equal to the second value.


$a = 1;
if (!isset($a)){ // If the variable $a is NOT set then...
echo '$a is not set'; // The expression is TRUE if it is NOT set
// Since there is no ELSE statement, nothing is displayed
if ($a != 0){
echo '$a does not equal zero';
$a does not equal zero