A beginners tutorial on programming PHP in Linux - Operators and Statements

A beginners tutorial on programming PHP in Linux - Operators and Statements
Page content

Introduction

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.

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!

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:

<?php

$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.

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:

<?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.

ELSE IF

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:

<?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.

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.

This post is part of the series: A Guide to Linux Programming with PHP - Series 2

This series continues from where the previous series finished. While the previous tutorials acted as an introduction, we shall gradually get more advanced as we go along while connecting what we have learnt about programming PHP to the real world.

  1. A Guide to Linux Programming with PHP - Operators and Statements