PHP Control Structures 1 - if and else
Written by Mike James   
Sunday, 21 April 2024
Article Index
PHP Control Structures 1 - if and else
Compound statements
Example

Getting to grips with programming or a new language is a matter of mastering the flow of control. This is the key idea in programming and understanding it makes the difference between a programmer and a non-programmer.

Introduction to PHP

phpcover

Contents

  1. Getting started
    Getting Started With NetBeans PHP - Local Projects
  2. What PHP Does
  3. PHP Variables and Expressions for Complete Beginners
  4. PHP Control Structures 1 - if and else
  5. PHP Control Structures 2 - switch and elseif
  6. PHP loops
  7. Advanced loops
  8. Functions
  9. Ten minutes to PHP objects
  10. PHP Inner Functions And Closure
  11. NetBeans Remote Projects and debugging

If you already know about flow of control, because you program in another language, then skip to the summaries of PHP's control structures - this is aimed at the non-programmer.

It is also assumed that you are happy with the ideas of variables and expressions – if not see PHP variables and expressions for complete beginners.

Banner

Default flow of control

A program is a list of instructions and when you run the program these instructions are obeyed one after another, nearly always reading from left to right and top to bottom of the list. This is the default flow of control - each instruction has its moment to be obeyed and hence to be in control of the computer.

In PHP the default flow of control is what you get, by default. For example in:

 echo "Hello";
echo "World";
echo "final instruction";

each of the echo instructions are obeyed one after the other from the top of the list to the bottom.

The default flow of control is so natural that it's easy to just not notice it. If you take your finger and follow down the instructions in the order in which they are obeyed then you will trace out a straight line – this is the "shape" of the default flow of control.

 

default

Selecting when to do something – the if

The first change to the default flow of control is when you only want something to happen if a condition is satisfied. In everyday life we often encounter conditional instructions:

"if the coffee is black then add sugar"

In this case the instruction "add sugar" is only obeyed if the condition, i.e. "the coffee is black", is true. Notice that this conditional is part of the default flow of control in the sense that it is part of the list of instructions:

pour the coffee
if the coffee is black then add sugar
drink the coffee

In PHP a conditional or if statement is written

 if(condition)instruction

and the instruction is only carried out if the condition is true. In a syntax that is closer to PHP you might write the coffee conditional as:

if(coffee is black) add sugar;

Of course "coffee is black" isn't a PHP conditional and "add sugar" isn't a PHP instruction – but the idea still holds good.

Conditions

To make use of if statements and other similar control statements we need to find out how to write a conditional in PHP. In most cases the sort of condition you want to write is a comparison between two values. You want to know if the value in the variable $a is zero or if it is bigger than the value in variable $b, for example.

To make these comparisons possible there are a range of suitable operators which can be used to form conditional or Boolean expressions which evaluate to true or false.

For example, $a==$b is true if the variables store the same value. Notice that the comparison operator is a double equals sign that is = followed by another = without a space. Other comparison operators are fairly obvious; $a<$b is only true if the value in $a is less than the value in $b and $a>$b is only true if the value in $a is greater than $b and so on. The following table summarizes the most commonly used:

$a==b$ true if equal value
$a!=$b or $a<>$b true if not equal
$a<$b true if $a less than $b
$a>$b true if $a greater than $b
$a<=$b true if $a less than
or equal to $b
$a>=$b true if $a greater than
or equal to $b

If you look at the documentation you will discover that there is also a triple equals which is true if the two values are equal and of the same type, i.e. no automatic type conversion is performed before the comparison.

As well as simple numerical comparisons you can also test to see if strings are equal or not. For example if you store some text in a variable:

 a$= "APPLE";

then the condition: a$== "APPLE" is true. You can use other comparison operators such as < and > with strings but exactly how these work is complicated and most of the time you only test strings for equality using == or inequality using != or <>.

For example, assuming the previous assignment:

 a$ != "ORANGE"

is true, i.e. a$ isn't equal to ORANGE and

 a$ == "ORANGE"

is false.

<ASIN:0672329166>

<ASIN:0596157134>

<ASIN:1593271735>

<ASIN:1590598628>



Last Updated ( Sunday, 21 April 2024 )