Управляющие структуры

Zephir реализует упрощенный набор управляющих структур, присутствующих в подобных ему языках, таких как C, PHP и т.п.


Оператор if

if statements evaluate an expression, executing the following block if the evaluation is true. Фигурные скобки обязательны. Оператор if может иметь необязательное предложение else. При необходимости проверить последовательно несколько условий возможно каскадирование (вложенные конструкции if/else):

if false {
    echo "false?";
} else {
    if true {
        echo "true!";
    } else {
        echo "neither true nor false";

Вы также можете использовать elseif:

if a > 100 {
    echo "to big";
} elseif a < 0 {
    echo "to small";
} elseif a == 50 {
    echo "perfect!";
} else {
    echo "ok";

Скобки в оцениваемом выражении необязательны:

if a < 0 { return -1; } else { if a > 0 { return 1; } }

Оператор switch

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";

    case 2:
    case 4:
        echo "even items";

        echo "unknown items";


Цикл while

while denotes a loop that iterates as long as its given condition evaluates as true:

let counter = 5;
while counter {
    let counter -= 1;

Цикл loop

In addition to while, loop can be used to create infinite loops:

let n = 40;
loop {
    let n -= 2;
    if n % 5 == 0 { break; }
    echo x, "\n";

Цикл for

A for is a control structure that allows to traverse arrays or strings:

for item in ["a", "b", "c", "d"] {
    echo item, "\n";

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:

let items = [1, 2, 3, 4, 5];

for value in reverse items {
    echo value, "\n";

A for loop can be used to traverse string variables:

string language = "zephir"; char ch;

for ch in language {
    echo "[", ch ,"]";

In reverse order:

string language = "zephir"; char ch;

for ch in reverse language {
    echo "[", ch ,"]";

A standard for that traverses a range of integer values can be written as follows:

for i in range(1, 10) {
    echo i, "\n";

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
for key, _ in data {
    echo key, "\n";

Оператор break

break ends execution of the current while, for or loop statement:

for item in ["a", "b", "c", "d"] {
    if item == "c" {
        break; // exit the for
    echo item, "\n";

Оператор continue

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.

let a = 5;
while a > 0 {
    let a--;
    if a == 3 {
    echo a, "\n";

Оператор 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.

if file_exists(path) {
    require path;

Оператор 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:

let price = 1.00, realPrice = price, status = false;