Chapter 11. Control Structures

Table of Contents
if
else
elseif
Alternative syntax for control structures
while
do..while
for
foreach
break
continue
switch
require()
include()
require_once()
include_once()

Any PHP script is built out of a series of statements. A statement can be an assignment, a function call, a loop, a conditional statement of even a statement that does nothing (an empty statement). Statements usually end with a semicolon. In addition, statements can be grouped into a statement-group by encapsulating a group of statements with curly braces. A statement-group is a statement by itself as well. The various statement types are described in this chapter.

if

The if construct is one of the most important features of many languages, PHP included. It allows for conditional execution of code fragments. PHP features an if structure that is similar to that of C:

if (expr)
    statement
     

As described in the section about expressions, expr is evaluated to its truth value. If expr evaluates to TRUE