Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

zend.assertions

The zend.assertions directive controls compilation and execution of assert expressions.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

php -d zend.assertions=1 script.php

The zend.assertions directive controls compilation and execution of assert expressions. Mode 1 generates and runs the assertion code, mode 0 generates it but jumps around it, and mode -1 omits it completely. Switching to or from -1 requires startup configuration, so an ini_set call cannot restore omitted code. The example starts three CLI processes and counts an expression side effect.

function executeMode(int $mode, string $source): string {
    $command = [PHP_BINARY, "-n", "-d", "zend.assertions=$mode", "-r", $source];
    $process = proc_open($command, [1 => ["pipe", "w"], 2 => ["pipe", "w"]], $pipes);
    if (!is_resource($process)) { throw new RuntimeException("start failed"); }
    $answer = stream_get_contents($pipes[1]);
    $error = stream_get_contents($pipes[2]);
    fclose($pipes[1]); fclose($pipes[2]);
    if (proc_close($process) !== 0) { throw new RuntimeException($error); }
    return $answer;
}
foreach ([1, 0, -1] as $mode) {
    $code = '$n = 0; assert(++$n === 1); echo $n;';
    echo $mode, " ", executeMode($mode, $code), PHP_EOL;
}
1 1
0 0
-1 0

Examples

<?php
function executeMode(int $mode, string $source): string {
    $command = [PHP_BINARY, "-n", "-d", "zend.assertions=$mode", "-r", $source];
    $process = proc_open($command, [1 => ["pipe", "w"], 2 => ["pipe", "w"]], $pipes);
    if (!is_resource($process)) { throw new RuntimeException("start failed"); }
    $answer = stream_get_contents($pipes[1]);
    $error = stream_get_contents($pipes[2]);
    fclose($pipes[1]); fclose($pipes[2]);
    if (proc_close($process) !== 0) { throw new RuntimeException($error); }
    return $answer;
}
foreach ([1, 0, -1] as $mode) {
    $code = '$n = 0; assert(++$n === 1); echo $n;';
    echo $mode, " ", executeMode($mode, $code), PHP_EOL;
}

Result

1 1
0 0
-1 0

Sources