4-5: PHP programming language basics – conditional statements – if, elseif, else

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.

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.

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:

To check if two elements are NOT equal, we can use the “not equal to” operator !=

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.

To check if two elements are NOT identical, we can use the “not identical to” operator !==

>, <, >= 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

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

Leave a Reply

Your email address will not be published. Required fields are marked *