-3

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?

13
  • 2
    A programming language consists of multiple parts, you find all of them documented in the manual, but you need to take the manual as one item, not just links into the manual that only cover a fraction. Read it from cover to end and you should get what you look for. Commented May 2, 2012 at 19:55
  • 1
    What, exactly, are you expecting to gain from this? What's your point? Commented May 2, 2012 at 19:55
  • 1
    @Sampo Of course it's "official PHP feature". It's clearly demonstrated in the manual. Commented May 2, 2012 at 19:59
  • 4
    No-one ever reads the introductions. — php.net/control-structures.intro Commented May 2, 2012 at 20:00
  • 1
    @Sampo I welcome your input on how/where you think the manual could be improved with respect to the trouble you've been having. Feel free to drop into chat or email me (my username here @ php.net). Input on troublesome areas of the manual is always welcomed. Commented May 2, 2012 at 20:32

3 Answers 3

9

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

Sign up to request clarification or add additional context in comments.

11 Comments

Alright, so there is some official documentation.
"Any PHP script is built out of a series of statements. … statements can be grouped into a statement-group by encapsulating a group of statements with curly braces." (emphasis mine) — From: php.net/control-structures.intro
@salathe: Okay. But that doesn't cover closing and opening the PHP tag.
As I read it, the question was about curly braches instead of the "alternative syntax". The OP wanted to see documentation of what the latter is an alternative to.
@salathe: Did you look at the first code sample?
|
2

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.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.