Advertisement
Tech

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

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.

By Berry van der Linden
Desk Tech
Reading time 4 min read
Word count 679
Linux Computing Linux commands
A beginners tutorial on programming PHP in Linux - Operators and Statements
Advertisement
Quick Take

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.

On this page

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:

Advertisement
  • 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.

    Advertisement

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:

Advertisement

If x is true, then do y

If x is false, then skip x

Advertisement

The syntax of If looks like this:

if (condition) {

Advertisement

//any PHP here gets executed

}

Advertisement

Below is an example if a PHP If statement:

<?php

Advertisement

$Day = “Monday”; // set variable

if ($Day == “Monday”) {

Advertisement

print (“Monday’s suck!”);

}

Advertisement

?>

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

Advertisement

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.

Advertisement

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

Advertisement

ELSE if x is false, then do z

Here is the idea in pseudo code:

Advertisement

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
Keep Exploring

More from Tech

Filed under
Linux Computing
More topics
Linux commands
Advertisement