tags:

views:

161

answers:

5
if(true):
    echo 'good';
endif;

I've only see it in PHP today!

I've always been using:

if(true)
{
   echo 'good';
}
+10  A: 

It is the Alternative syntax for control structures.


Quoting that page of the manual :

PHP offers an alternative syntax for some of its control structures; namely, if, while, for, foreach, and switch. In each case, the basic form of the alternate syntax is to change the opening brace to a colon (:) and the closing brace to endif;, endwhile;, endfor;, endforeach;, or endswitch;, respectively.


I don't often see it used in "pure PHP" files, but it's quite often used when PHP is mixed with HTML in the same file -- i.e. when used as a templating language.

Pascal MARTIN
Alternativ control structures are a nice feature but often a pain in the ass because no IDE will provide any help in navigating these structures by highlighting. At least mine doesn't.
Pekka
What IDE are you using?
Justin Ethier
+1  A: 

You can interlace the former with html very easily.

http://php.net/manual/en/control-structures.alternative-syntax.php

Matt
+1  A: 

It's exactly the same. I often use such syntax in templates - it's much more readable then (in my opinion). Here's more: Alternative syntax for control structures

Crozin
A: 

The PHP notation:

if(x=1):
  dosomething();
elseif(x=2):
  dosomethingelse();
  andanotherthing();
else:
  doesntmatcheither();
endif;

is also related to

$var=(query?true:false);

or in even more shorthand

$var=query?true:false;

But I personally don't like omitting the parenthesis; call me paranoid ;)

Neil Highley
How exactly is that related to each other? It has nothing to do with the actual question.
poke
Also, because you are using single `=`'s, `x` is set to 1 and evaluated.
Alexsander Akers
A: 

same effect, no diference and in my personal opinion the offer better readble code and clean syntax, also the end-like closing keyword might help in nested control structures, to indicate which one is closing instead of guessing from a bunch of braces.

jgemedina