php8.5
Home/ Manual/ language/ Control Structures

Control Structures

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 or 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.

Introduction

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 or 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.

See Also

The following are also considered language constructs even though they are referenced under functions in the manual.

If Else Elseif Alternative-syntax While Do-while For Foreach Break Continue Switch Match Declare Return Require Include Require-once Include-once Goto

In this section

Alternative syntax for control structures

In the above example, the HTML block "A is equal to 5" is nested within an if statement wr...

break

break ends execution of the current for, foreach, while, do-while or switch structure.

continue

continue is used within looping structures to skip the rest of the current loop iteration...

declare

The directive section allows the behavior of the declare block to be set. Currently only t...

do-while

do-while loops are very similar to while loops, except the truth expression is checked at...

else

elseif/else if

There may be several elseifs within the same if statement. The first elseif expression (if...

for

The first expression (expr1) is evaluated (executed) once unconditionally at the beginning...

foreach

The first form traverses the value given by iterable_expression. On each iteration, the va...

goto

What's the worse thing that could happen if you use goto? Image courtesy of xkcd

if

As described in the section about expressions, expression is evaluated to its Boolean valu...

include

The include expression includes and evaluates the specified file.

include_once

The include_once expression includes and evaluates the specified file during the execution...

match

The match expression branches evaluation based on an identity check of a value. Similarly...

require

require is identical to include except upon failure it will also produce an Error exceptio...

require_once

The require_once expression is identical to require except PHP will check if the file has...

return

return returns program control to the calling module. Execution resumes at the expression...

switch

The switch statement is similar to a series of if statements on the same expression. In si...

while

The meaning of a while statement is simple. It tells PHP to execute the nested statement(s...

Source: language/control-structures.xml · from the official PHP manual (php/doc-en)