Control Structures¶
Zephir implements a simplified set of control structures present in similar languages like C, PHP etc.
Conditionals¶
If Statement¶
if
statements evaluate an expression, executing the following block if the evaluation is true
. Braces are required. An if
can have an optional else
clause, and multiple if
/else
constructs can be chained together:
if false {
echo "false?";
} else {
if true {
echo "true!";
} else {
echo "neither true nor false";
}
}
elseif
clauses are also available:
if a > 100 {
echo "to big";
} elseif a < 0 {
echo "to small";
} elseif a == 50 {
echo "perfect!";
} else {
echo "ok";
}
Parentheses in the evaluated expression are optional:
Switch Statement¶
A switch
evaluates an expression against a series of predefined literal values, executing the corresponding case
block or falling back to the default
block case:
switch count(items) {
case 1:
case 3:
echo "odd items";
break;
case 2:
case 4:
echo "even items";
break;
default:
echo "unknown items";
}
Loops¶
While Statement¶
while
denotes a loop that iterates as long as its given condition evaluates as true
:
Loop Statement¶
In addition to while
, loop
can be used to create infinite loops:
For Statement¶
A for
is a control structure that allows to traverse arrays or strings:
Keys in hashes can be obtained by providing a variable for both the key and value:
let items = ["a": 1, "b": 2, "c": 3, "d": 4];
for key, value in items {
echo key, " ", value, "\n";
}
A for
loop can also be instructed to traverse an array or string in reverse order:
A for
loop can be used to traverse string variables:
In reverse order:
A standard for
that traverses a range of integer values can be written as follows:
To avoid warnings about unused variables, you can use anonymous variables in for
statements, by replacing a variable name with the placeholder _
:
Use the key but ignore the value¶
Break Statement¶
break
ends execution of the current while
, for
or loop
statement:
Continue Statement¶
continue
is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation, and then the beginning of the next iteration.
Require¶
The require
statement dynamically includes and evaluates a specified PHP file. Note that files included via Zephir are interpreted by Zend Engine as normal PHP files. require
does not allow Zephir code to include other Zephir files at runtime.
Let¶
The let
statement is used to mutate variables, properties and arrays. Variables are by default immutable and this instruction makes them mutable for the duration of the statement:
let name = "Tony"; // simple variable
let this->name = "Tony"; // object property
let data["name"] = "Tony"; // array index
let self::_name = "Tony"; // static property
Also this instruction must be used to increment/decrement variables:
let number++; // increment simple variable
let number--; // decrement simple variable
let this->number++; // increment object property
let this->number--; // decrement object property
Multiple mutations can be performed in a single let
operation: