PHP Control Structures



index
Disabled back button Next Section
printable version

Section 0: Module Objectives or Competencies
Course Objective or Competency
The student will be able to write basic PHP applications in order to connect to and manipulate a database using PHP.
Section 1: Overview

PHP, like any other programming language, provides facilities that enable you to affect the flow of control.

Such statements are called control structures.

Some of the material in this section came from this chapter.

Section 2: Selection

Like JavaScript, PHP provides single selection, double selection, and multiple selection structures.

In fact, the syntax looks very much like that of JavaScript.

if Statement

This control structure lets us tell PHP to execute a set of statements only if some condition is met.

Flowchart of if statement.

Here's what an if statement looks like in PHP code:

if (condition)
{
// conditional code to be executed if condition is true
}

if-else Statement

This control structure lets us tell PHP to execute a set of statements if some condition is met, or a different set of statements if the condition is not met.

Flowchart for if/else statement.

Here's what an if-else statement looks like in PHP code:

if (condition)
{
// conditional code to be executed if condition is true
}
else
{
// conditional code to be executed if condition is false
}

switch Statement

This control structure can be used in place of an if statement with multiple else if clauses in which one expression is tested for equality with several values.

Flowchart for switch statement.

Here's what a switch statement looks like in PHP code:

switch (expression)
{
case label₁:
// conditional code to be executed if condition₁ is true
break;
case label₂:
// conditional code to be executed if condition₂ is true
break;


case labelₓ:
// conditional code to be executed if conditionₓ is true
break;
default:
// default code to be executed if no conditions are satisfied
}

The values in the case label may be literal values or expressions.

Section 3: Iteration

Again, like JavaScript, PHP offers pre-test (while) loops, post-test (do-while) loops, and count-controlled (for) loops.

while

This control structure tells PHP to execute a set of statements as long as the condition is true.

Flowchart for while statement.

Here's what a while statement looks like in PHP code:

while (condition)
{
// statement(s) to execute repeatedly as long as condition is true
}

The while loop works a lot like an if statement.

do-while

The do-while loop is similar to the while loop except that the conditional expression is tested at the end of the loop.

Flowchart for do/while statement.

Here's what a do-while statement looks like in PHP code:

do
{
// statement(s) to execute repeatedly as long as condition is true
}
while (condition);

for

As you know, count-controlled loops count through a series of values until some condition is met.

Flowchart for for statement.

Here's what a for statement looks like in PHP code:

for (counterDeclaration; condition; counterUpdate)
{
// statement(s) to execute repeatedly as long as condition is true
}

Section 4: Resources