CKTIS / PHP
Keyword endforeach
End a colon-style foreach block in a template or PHP file.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
foreach ($items as $value): /* body */ endforeach;End a colon-style foreach block in a template or PHP file. An executed pass assigns the loop variable. An empty traversal leaves an existing binding alone and leaves an initially undefined variable undefined. After a reference loop, explicitly unset the variable before reusing it or it can overwrite the last element.
$items=[1,2];
foreach($items as &$value): $value*=2; endforeach;
unset($value); $value=99; echo json_encode($items);
foreach([] as $fresh): endforeach;
echo PHP_EOL,json_encode(array_key_exists("fresh",get_defined_vars()));
[2,4]
false
Examples
<?php
$items=[1,2];
foreach($items as &$value): $value*=2; endforeach;
unset($value); $value=99; echo json_encode($items);
foreach([] as $fresh): endforeach;
echo PHP_EOL,json_encode(array_key_exists("fresh",get_defined_vars()));
Result
[2,4] false