Pin Me

A Guide to Linux Programming with PHP - Operators and Statements

written by: Berry van der Linden•edited by: Michael Dougherty•updated: 9/2/2009

When programming PHP, or any other language, operators enable us to get creative with variables and values. During this PHP tutorial and the next we will talk about what PHP operators are, what they can do, while learning how to compare variable values and use arithmetic & Boolean operations.

  • slide 1 of 6


    In the last PHP tutorial of the series we studied associative and multidimensional arrays. This series is a continuation from where we left-off and the idea is that it will gradually get more advanced as we go along. We'll begin by learning about operators in PHP.

  • slide 2 of 6

    PHP, Operators and Dynamic Content

    The purpose of dynamic websites to have a site that intelligently updates as a result of certain factors - input from the user, user properties (operating system/browser) or triggers from other locations, for example we could use PHP to do the following:

    • When a user enters their details, such as a telephone number or an email address, the site can validate them to check that they are the correct format.
    • Calculate the shipping costs of the items in the basket of a customer on an online store.

    The possibilities when programming PHP are endless!

  • slide 3 of 6

    The IF Statement

    If, Else, and Elseif statements are very important! Without them as well as logical and comparison operators, you site would not be very clever at all! We'll start by taking a look at the if statement, the most important of all statements! If statements allow you to program:

    If x is true, then do y

    If x is false, then skip x

    The syntax of If looks like this:

    if (condition) {

    //any PHP here gets executed


    Below is an example if a PHP If statement:


    $Day = "Monday"; // set variable

    if ($Day == "Monday") {

    print ("Monday's suck!");



    An important this to learn about the operators = and == is that they are totally different!

    if ($Day = "Monday") and if ($Day == "Monday")

    The operator = means that $Day will be set as Monday, whereas, == should be read as "becomes equal to" - The value of $Day becomes equal to "Monday". We will look at these more closely in a later PHP tutorial.

  • slide 4 of 6

    Or ELSE What?!

    Else adds another layer to the If operator, this is true in most languages and not just when programming PHP. Here is how it works:

    If x is true, then do y

    ELSE if x is false, then do z

    Here is the idea in pseudo code:

    if (condition) {

    // PHP here gets run

    } else {

    // if above is false, this PHP get executed


    Here is Else in PHP:


    $Day = "Friday";

    if ($Day == "Friday") {

    print ("Yay! It's Friday");

    } else {

    print ("Hang in there!");



    The above code is a very common statement and you will see it lots in the future! Notice the location of the curly brackets, as they indicate the start and end of a statement. A { indicates the beginning of a statement, while the } ends a statement.

  • slide 5 of 6


    Now that we've looked at If and Else we can take a look at their child - Elseif. While Else will execute code if the condition of the If statement is fasle, elseif executes code if a separate condition is true, confused? Here is an example:

    If x is true, then do y

    ELSEIF a is true, then do b

    Here it is in PHP:


    $Day = "Monday";

    if ($Day == "blue") {

    print ("Unlucky!");

    } elseif ($Day = "Wednesday") {

    print ("Half way!"); }


    See the subtle differece between ELSEIF and ELSE in PHP? It would be possible to add an ELSE to print a message if $Day was not equal to Monday or Wednesday.

  • slide 6 of 6

    I hope you have enjoyed learning about operators in PHP. Next time we extend our operator knowledge by looking at PHP comparative and logical operators.