10

I've been working on PHP for some time but today when I saw this it came as new to me:

if(preg_match('/foo.*bar/','foo is a bar')):
        echo 'success ';
        echo 'foo comes before bar';

endif;

To my surprise it also runs without error. Can anyone enlighten me?

Thanks to all :)

codaddict
  • 410,890
  • 80
  • 476
  • 515
Joseph
  • 241
  • 2
  • 9
  • 5
    as the answers below have said, this is an alternative syntax. It's also awfully painful to read IMO, please avoid this in your own code. – nickf May 07 '10 at 13:36
  • 3
    this kind of syntax is used in templates when you often open and close and you use html in between. It is horrible and unreadable even in those cases, and should be avoided altogether. – o0'. May 07 '10 at 13:48
  • 1
    @Lo'oris, I disagree with you. Using it in your templates can improve readability if you ask me: see http://stackoverflow.com/questions/2788891/strange-php-syntax/2789008#2789008 for an example. – Marijn Huizendveld May 07 '10 at 13:50
  • 1
    I agree with those who recommend against it. Even in templates, as Lo'oris mentioned, it does almost nothing for readability. But the main problem I have with it is that I haven't found an IDE which highlights matching if/endif or foreach/endforeach or any other alternate syntax tags. For that reason alone it should be avoided. Netbeans will highlight matching braces even if they span blocks, but it hasn't a clue how to do so with the alternates. I think the fact that this question is being asked is further evidence that it should be avoided, to prevent confusion. – aw crud May 07 '10 at 13:56

5 Answers5

28

This is PHP's Alternative syntax for control structures.

Your snippet is equivalent to:

if(preg_match('/foo.*bar/','foo is a bar')) {
        echo 'success ';
        echo 'foo comes before bar';
}

In general:

if(cond):
...
...
endif;

is same as

if(cond) {
...
...
}
codaddict
  • 410,890
  • 80
  • 476
  • 515
14

That style of syntax is more commonly used when embedding in HTML, especially for template/display logic. When embedded this way, it's a little easier to read than the curly braces syntax.

<div>
<? if ($condition): ?>
  <ul>
    <? foreach($foo as $bar): ?>
        <li><?= $bar ?></li>
    <? endforeach ?>
  </ul>
<? endif ?>
</div>

Versus:

<div>
<? if ($condition) { ?>
  <ul>
    <? foreach($foo as $bar) { ?>
      <li><?= $bar ?></li>
    <? } ?>
  </ul>
<? } ?>

The verbose end tags make it a little easier to keep track of nested code blocks, although it's still mostly personal preference.

Harold1983-
  • 3,131
  • 1
  • 21
  • 22
5

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

Works for if, for, while, foreach, and switch. Can be quite handy for mixing PHP and HTML.

binaryLV
  • 8,594
  • 2
  • 36
  • 42
1

You can read about it in Alternative syntax for control structures in the PHP manual. Reformatted, the code you posted looks like this:

if (preg_match('/foo.*bar/','foo is a bar')):
    echo 'success ';
    echo 'foo comes before bar';
endif;

This code is equivalent to:

if (preg_match('/foo.*bar/','foo is a bar')) {
    echo 'success ';
    echo 'foo comes before bar';
}

This syntax is available for several other control structures as well.

if ( condition ):
  // your if code
elseif ( other_condition ):
  // optional elseif code
else:
  // optional else code
endif;

while ( condition ):
  // your while code
endwhile;

for ( condition ):
  // your for code
endfor;

foreach ( condition ):
  // your foreach code
endforeach;

switch ( condition ):
  // your switch code
endswitch;
artlung
  • 30,810
  • 16
  • 66
  • 118
0

It's the equivalent of:

if(preg_match('/foo.*bar/','foo is a bar')):
 echo 'success ';
 echo 'foo comes before bar';
endif;

which is equivalent to:

if(preg_match('/foo.*bar/','foo is a bar')){
    echo 'success ';
    echo 'foo comes before bar';
}

The wisdom of supporting non-standard conditional syntax is obviously questionable.

Satanicpuppy
  • 1,539
  • 8
  • 11