PHP Control Structures Alternative syntax for control structures

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

PHP provides an alternative syntax for some control structures: if, while, for, foreach, and switch.

When compared to the normal syntax, the difference is, that the opening brace is replaced by a colon (:) and the closing brace is replaced by endif;, endwhile;, endfor;, endforeach;, or endswitch;, respectively. For individual examples, see the topic on alternative syntax for control structures.

if ($a == 42):
    echo "The answer to life, the universe and everything is 42.";
endif;

Multiple elseif statements using short-syntax:

if ($a == 5):
    echo "a equals 5";
elseif ($a == 6):
    echo "a equals 6";
else:
    echo "a is neither 5 nor 6";
endif;

PHP Manual - Control Structures - Alternative Syntax



Got any PHP Question?