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

Community
  • 1
  • 1
  • 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. – hakre May 02 '12 at 19:55
  • 1
    What, exactly, are you expecting to gain from this? What's your point? – ceejayoz May 02 '12 at 19:55
  • @ceejayoz My point is `is that even official PHP feature` and should I use `:` and `endif;` like structures instead of using `{` and `}` – Sampo Sarrala - codidact.org May 02 '12 at 19:58
  • They're identical. The most important point is that they're within `` tags if it is within output sent to the browser. – Malovich May 02 '12 at 19:59
  • 1
    @Sampo Of course it's "official PHP feature". It's clearly demonstrated in the manual. – user229044 May 02 '12 at 19:59
  • @Sampo Given that `:` and `endif;` are documented as an **alternative** syntax, I'd be pretty comfortable using the normal syntax. – ceejayoz May 02 '12 at 20:00
  • i've deleted my answer as it covered the php manual links in your question. why is that not source enough for a well documented feature? – Hajo May 02 '12 at 20:00
  • 4
    No-one ever reads the introductions. — http://php.net/control-structures.intro – salathe May 02 '12 at 20:00
  • @Hajo: Because that's not where it's documented. – Ignacio Vazquez-Abrams May 02 '12 at 20:02
  • Ok, it's now clear as [control-structures.intro](http://www.php.net/control-structures.intro) and [language.basic-syntax.phpmode.php](http://www.php.net/language.basic-syntax.phpmode.php) together documented it. So it is official and it is documented but not demonstrated as many other php features. – Sampo Sarrala - codidact.org May 02 '12 at 20:18
  • 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](http://chat.stackoverflow.com/rooms/11/php) or email me (my username here @ php.net). Input on troublesome areas of the manual is always welcomed. – salathe May 02 '12 at 20:32
  • @meagar If it really is _clearly demonstrated in the manual_ can you drop link to this demonstration? Your help could be very useful as now I am looking at where and how should php manual improved. Separating `{` and `}` by `` within conditional statements is clear for anyone who has some experience with php but I think that it may not be so clear if you have just started learning it (maybe without any other programming experience). – Sampo Sarrala - codidact.org May 02 '12 at 21:09
  • @Sampo See [**Example #3 Mixing both HTML and PHP modes**](http://ca.php.net/manual/en/tutorial.useful.php#example-6) from the [introductory tutorial](http://ca2.php.net/tut.php). Took me 10 seconds to find it. – user229044 May 03 '12 at 15:04

3 Answers3

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

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
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.

user229044
  • 232,980
  • 40
  • 330
  • 338
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.

Malovich
  • 931
  • 1
  • 5
  • 15