Domanda

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?

È stato utile?

Soluzione

...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

Altri suggerimenti

The manual on control structures is very clear. This is fundamentally identical to other C-style languages which use {} to denote blocks.

These control structures work with the <?php ?> to switch context in and out of HTML-mode exactly as you would expect.

As far as I know, there is no 'real documentation' when it comes to the first format.

The reason is, because it is assumed that you already understand how php works in conjunction to HTML.

What has to be understood is that php represents a dynamic insertion and preprocessing of code on the server before it is sent to the browser. So, the server looks at your code, and either prints the respective HTML to the browser, or it doesn't. It literally was created for this very purpose.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top