Chapter 3. Flow Control

In this lesson you will learn about the conditional and looping constructs that allow you to control the flow of a PHP script.

In this chapter we'll look at two types of flow control: conditional statements, which tell your script to execute a section of code only if certain criteria are met, and loops, which indicate a block of code that is to be repeated a number of times.

Conditional Statements

A conditional statement in PHP begins with the keyword if, followed by a condition in parentheses. The following example checks whether the value of the variable $number is less than 10, and the echo statement displays its message only if this is the case:

$number = 5;
if ($number < 10) {
  echo "$number is less than ten";
}

The ...

Get Sams Teach Yourself PHP in 10 Minutes now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.