In the flow of an application it is a common situation that some portions of code should be executed only if particular conditions are met. We can exert this control by using one or more conditional statements.
The if statement
Depending on the situation, an “if” statement could be all we need: if a particular situation is met, we will execute some special code, otherwise we just do nothing special and continue executing the code that follows the “if” statement.
In the following example we see how to implement a set a banned IP addresses that will be unable to see the contents of a particular web page, based on a simple if statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php $header = file_get_contents("html/header.html"); $footer = file_get_contents("html/footer.html"); $page_contents = " Hello, world! "; $denied_contents = " Sorry, you are not allowed to see the contents of this page "; $visitor_ip = $_SERVER['REMOTE_ADDR']; // We store the visitor IP to the $visitor_ip variable // We have a list of IP addresses that should not access this web page contents $banned_ips = array("160.80.22.35","160.110.22.36","160.110.22.37","160.110.22.38"); // We now check if the visitor's IP is among the banned IP addresses foreach($banned_ips as $banned_ip){ if($visitor_ip == $banned_ip){ // if we get a match we output a special page and exit the PHP script with the "die" function die($header.$denied_contents.$footer); } } // The following will be executed only if we did not find a match // between the visitor's IP and one of the banned IP addresses echo $header.$page_contents.$footer; ?> |
This example is worth a few comments:
- In contrast to the equal to sign =, which is an “assignment operator”, the double equal to sign == is a PHP “comparison operator”. It will return “true” if the elements on each side are equal and “false” if they are not. Comparison operators are used often within conditional statements to check if a certain condition is met. We will discuss them in more detail below.
- The die() function is used to print a message, passed to the function as argument, and terminate the execution of a PHP script.
- This is a nice example of a dynamic page whose contents change depending on a particular condition (in this case the visitor’s IP address)
The elseif and else statements
If we need to check for two or more precisely defined alternative conditions, we can use an if statement followed by one or more elseif statements.
if there is a code we wish to execute when neither the if condition nor any of the alternative elseif conditions are met, we can also add an (optional) else statement at the end.
In other cases we can manage our conditions by using an if directly followed by an else, omitting the elseif. It really depends on how how conditions are structured and should be applied within our code.
In the following example we see how to show different contents to visitors from different IP addresses in a webpage based on an set of if, elseif and else statements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<?php $header = file_get_contents("html/header.html"); $footer = file_get_contents("html/footer.html"); // For everyone, but the banned IP, to see $page_contents = " Hello, world! \n"; // Just for Brian, a friend with a known IP address (160.110.22.130) $brian_message = " Hey Brian, the key is under to stone to the left side of the door \n"; // Message to show to the banned IP $denied_contents = " Sorry, you are not allowed to see the contents of this page \n"; $visitor_ip = $_SERVER['REMOTE_ADDR']; // We store the visitor IP to the $visitor_ip variable $banned_ip = "160.110.22.35"; // We have just one banned IP this time $brian_ip = "160.110.22.130"; // Brian's IP address // We now issue different pages depending on the visitor's IP address if($visitor_ip == $banned_ip){ // if the IP is the banned one we output a special page echo $header.$denied_contents.$footer; } elseif($visitor_ip == $brian_ip){ // Just for brian echo $header.$page_contents.$brian_message.$footer; } else{ // For everyone else echo $header.$page_contents.$footer; } ?> |
PHP comparison operators
Within conditional statements, whether the condition to be met is true or false is often checked by using comparison operators. In the previous example we used ==, however there are several others.
The == equality operator
The == equality operator checks if the value of two elements is the same. This is however different from checking if they are identical, that is if they have the same value and also the same type.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<?php $a = 1; // Variable type is an integer $b = 1; $c = "1"; // Variable type is a string if($a == $b){ // This is of course true in this case echo " a equals b \n"; } if($a == $c){ // This is also true despite that the variable type of $a and $c is different echo " a equals c \n"; } if(false == 0){ // This is true, false and 0 are of different type (boolean and integer), but they both evaluate to false echo " the two elements are equal \n"; } if(13 == true){ // This is also true, 13 is not false, it is therefore true echo " the two elements are equal \n"; } if(false == 1){ // This is false echo " the two elements are equal \n"; } if(0 == true){ // This is false echo " the two elements are equal \n"; } ?> |
To check if two elements are NOT equal, we can use the “not equal to” operator !=
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $a = 1; $b = 2; if($a != $b){ echo " a is not equal to b "; { ?> |
The === identity operator
The triple equal to sign ===, also known as identity operator, checks if the value AND type of two variables are the same. That is, if they are identical. Again, in the realm of comparison operators, equality and identity are subtly distinct concepts. === is more restrictive than == as it checks both for value and type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?php $a = 1; // Variable type is an integer $b = 1; $c = "1"; // Variable type is a string if($a === $b){ // This is of course true in this case echo " a identical b \n"; } if($a === $c){ // False as the type of $a and $c is different, although the value is the same echo " a equals c \n"; } if(false === 0){ // False as the variable types are different echo " the two elements are equal \n"; } if(13 === true){ // False as the variable types are different echo " the two elements are equal \n"; } ?> |
To check if two elements are NOT identical, we can use the “not identical to” operator !==
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $a = 1; $b = "1"; if($a !== $b){ echo " a is not identical to b "; { ?> |
>, <, >= and <= operators
Those are typically used to compare numbers for which they do quite what you would expect. They can also be used to compare strings, on the basis of both length and alphabetical criteria, however we will not discuss this here.
- > Bigger than
- < Smaller than
- >= Bigger or equal
- <= Smaller or equal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php $a = 1; $b = 2; if($a > $b){ echo " a is bigger than b "; { elseif($a < $b){ echo " a is smaller than b "; } else{ echo " if a and b are numbers, likely a equals to b at this point "; } ?> |
For an exhaustive survey of all PHP operators, including comparison operators, we suggest you look at this excellent page on the W3Schools website as a reference.
Chapter Sections
[pagelist include=”435″]
[siblings]