Search code examples
phplanguage-features

Any documentation for <? if(..){ ?>...<? } ?> PHP control structure?


Is there any, official or unofficial, documentation for control structures like this one:

<?php if ($expression == true) { ?>
    <h3>Output</h3>
<?php } else { ?>
    <h3>Another case</h3>
<?php } ?>

I did not read docs when first time used it, just expected it to work and it worked well.

However, now I have read documentation about PHP control structures
at php.net/manual/en/control-structures.alternative-syntax.php
and php.net/manual/en/language.basic-syntax.phpmode.php
but still did not find anything that clearly tells that you can use { and } this way. And often php.net manual offers many good examples, at all angles, on almost every topic. (what i've read so far)

Is that even official PHP feature and if it is why there is no any clear documentation about it?

This is documented (using colon: with endif;) + sample code in manual as bonus:

<?php if ($expression == true): ?>
    This will show if the expression is true.
<?php else: ?>
    Otherwise this will show.
<?php endif; ?>

I am looking for real documentation, not someone's blog/forum posts.
I know it works as expected, I have always used it without any problems.
Somewhat related question Is this the correct way of putting HTML in PHP?


Solution

  • ...when the PHP interpreter hits the ?> closing tags, it simply starts outputting whatever it finds [...] unless in the middle of a conditional statement in which case the interpreter will determine the outcome of the conditional before making a decision of what which to skip over.

    source