PHP programming - Operators
Frank Yap

In this section we will look at why we need to use '===' comparison operator.
 
Resources

 
Lab Exercises

 
Exercise 1: Using == comparison operator


The following examples show proper uses of PHP operators. Study the following code and understand why error occurs.

Let's consider following functions.

function checkCurrentState ($channel) { 
if ($channel == 0){ 
return 5; 

if ($channel == 1){ 
return 0; 

if ($channel == 9){ // channel does not exist 
return false; 


 
 
function testChannel ($channel) { 
$state = checkCurrentState ($channel); 
if ($state == false) 
echo "Channel $channel does not exists.<br>"; 
else if ($state == 0) 
echo "Channel $channel is ready.<br>"; 
else if ($state > 0) 
echo "Channel $channel is running.<br>"; 

 
testChannel (0); 
testChannel (1); 
testChannel (9);
Output ---
Channel 0 is running.
Channel 1 does not exists.
Channel 9 does not exists.

The channel 1 report is incorrect.
 

Let's see what happens if we use '===' comparator in the function.
 

Exercise 2: Using === comparison operator

Study the following code and understand why error was corrected.
 

function testChannel_2 ($channel) { 
$state = checkCurrentState ($channel); 
if ($state === false) 
echo "Channel $channel does not exists.<br>"; 
else if ($state == 0) 
echo "Channel $channel is ready.<br>"; 
else if ($state > 0) 
echo "Channel $channel is running.<br>"; 

 
testChannel_2 (0); 
testChannel_2 (1); 
testChannel_2 (9);
Output ---
Channel 0 is running.
Channel 1 is ready.
Channel 9 does not exists.

The channel 1 report is now correct. We have to use '===' comparator where necessary.

Let's consider another case.


function checkNewMsg ($channel) { 
if ($channel == 0){ 
return "Hello World!"; 

if ($channel == 1){ 
return "0"; 

if ($channel == 9){ // channel does not exist 
return false; 


 
 
function testChannel_3 ($channel) { 
$msg = checkNewMsg ($channel); 
if ($msg == false) 
echo "Channel $channel does not exists.<br>"; 
else 
echo $msg . "<br>"; 

 
testChannel_3 (0); 
testChannel_3 (1); 
testChannel_3 (9);
Output ---
Hello World!
Channel 1 does not exists.
Channel 9 does not exists.

The channel 1 report is incorrect.
Let's see what happens if we use '===' comparator in the function.

function testChannel_4 ($channel) { 
$msg = checkNewMsg ($channel); 
if ($msg === false) 
echo "Channel $channel does not exists.<br>"; 
else 
echo $msg . "<br>"; 

 
testChannel_4 (0); 
testChannel_4 (1); 
testChannel_4 (9);
Output ---
Hello World!
0
Channel 9 does not exists.

The channel 1 report is now correct.

The statement "if ($msg == false)" means "if ((bool)$msg == (bool)false)",
and if $msg=="0", then the statement becomes "if ((bool)"0" == (bool)false)" which is true.

The statement "if ($msg === false)" means "if ((value & type)$msg === (value & type)false)",
and if $msg=="0", then the statement becomes
"if (((bool)"0" == (bool)false) && ((type)$msg == (type)false))" which is not true.

 
Homework Exercise

Start XAMPP Apache server.

Analyze the following code and understand why the outputs are as printed.
You are studying here the differences between && and 'and', and between || and 'or'.
The statement echo(true) prints 1 and the statement echo(false) prints blank.
Explain why you get the result.


function checkUser ($name) { 
if ($name == "Brown"){ 
return true; 

if ($name == "Smith"){ 
return true; 

return false; 

function reportUser ($name) { 
echo "$name, You are not a valid user.<br>"; 
}
$name = "Brown"; 
$result = checkUser ($name) and reportUser ($name); 
echo $result . "<br>"; 
$name = checkUser ($name) or reportUser ($name); 
echo $result . "<br>"; 
//--- 
$name = "Smith";  
$result = checkUser ($name) and reportUser ($name); 
echo $result . "<br>"; 
$result = checkUser ($name) or reportUser ($name); 
echo $result . "<br>"; 
//--- 
$name = "Young";  
$result = checkUser ($name) and reportUser ($name); 
echo $result . "<br>"; 
$result = checkUser ($name) or reportUser ($name); 
echo $result . "<br>"; 
echo "End of print.<br>";
Output ---
Brown, You are not a valid user.
1
1
Smith, You are not a valid user.
1
1

Young, You are not a valid user.

End of print.