If Statements

In order to decide something, you should use the if statement. Brackets are used to anclose the condition to check. The general format is:

if (condition) {
... do things if condition is true
}


The condition usually takes two forms. The first is to check whether a boolean variable is true or false. For example:

if ($logged_in_ok) {
    echo "Welcome to the system";
}


It is usual to indent the code between the curly braces so that is easy to see where an if statement starts and ends, The second form for an if condition is to check how one variable compares with another. For example:

if ($colour == "Red") {
    echo "You chose the colour red!";
}


It is important to note the == sign used to check to see if something is equal to something else. = is used to give something a value, == is used to check equality. You can also use the normal symbols to check greater than and less than for numeric values. For example:

if ($day > 31) {
    echo "Invalid day selected";
}


if ($total < 10) {
    echo "Choose at least 10 things";
}


if ($quantity == 0) {
    echo "Error - quantity cannot be zero";
}


The last two things to note are the uses of 'not equals to' and 'anything other than'. This is quickly shown in these examples:

if ($animal != "dog") {
    echo "You did not choose a dog!";
}


if ($chapter <> 1) {
    echo "This is not the first chapter";
}


PHP also allows you to do different things when a condition is not met. Use the else statement like this:

if ($chapter <> 1) {
    echo "This is not the first chapter";
} else {
    echo "This is the first chapter";
}


Next article: Arrays

Loops

There are two basic constructs to repeat things in PHP. The first is the 'while' loop. This will repeat a block of code while a condition remains true. The usual format for this is

$i = 1;
while ($i < 100) {
... stuff to be repeated
$i++;
}


There are three things to note here:
1. Set and use a variable to control the loop. $i is commonly used as the loop index
2. Increment the index within the loop. $i++ adds 1 to the value of $i. This means that $i starts at 1 before the loop starts, then goes to 2,3,4 etc. when $i++ is reached.
3. Finish the loop by specifying a condition. Here the loop would end when $i hits 100. Note that this means that the while loop will continue as long as the condition inside the brackets is true.

The second kind of loop is a 'for' loop. This looks like:

for ($i = 1; $i <= 100; $i++) {
... stuff to be repeated
{


Note:
1. This has the same elements as the while loop, but they are all included in the one statement - an initial value for the index, a condition to terminate the loop and the increment of the index.
2. This makes it much easier to loop through for a given number of times.

The main difference is that a 'while' loop is best used to repeat things when it is not know beforehand how many times the loop is needed, The 'for' loop is best used to repeat a set number of times.

Examples:

// Repeat until random number is different to one previously picked
$previous = 4;
$gotnew = false;
while (!$gotnew) {
    $number = mt_rand(1,10);
    if ($number <> $previous) {
        $gotnew = true;
    }
}


// Read string one chracter at a time
$string = "Welcome";
for ($i=1; $i<= strlen($string); $i++) {
    $new = substr($string, 0, $i);
    echo $new;
}

// Program will output W, then We, then Wel, then Welc, etc. upto Welcome


Next article: Arrays

Previous

The previous article tells you how to assign and use variables in PHP.

Next

The next screen gives you a tutorial on how to use arrays in PHP.
This tutorial tells you how to create arrays and how to manipulate them.

External Links

Summary

  • Use the if statement to check conditions
  • Remember to use == to check equality
  • Use while to repeat as long as a condition is true
  • Use for to repeat a certain number of times