Strange PHP syntax
Asked Answered
S

5

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 :)

Serpens answered 7/5, 2010 at 13:31 Comment(4)
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.Pourpoint
this kind of syntax is used in templates when you often open and close <?php?> and you use html in between. It is horrible and unreadable even in those cases, and should be avoided altogether.Imparity
@Lo'oris, I disagree with you. Using it in your templates can improve readability if you ask me: see https://mcmap.net/q/1036794/-strange-php-syntax/… for an example.Passade
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 <?php ?> 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.Basildon
Q
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.

Quaker answered 7/5, 2010 at 13:45 Comment(0)
H
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) {
...
...
}
Heliochrome answered 7/5, 2010 at 13:33 Comment(2)
I believe you... but why in Satan's good name would you want to do that?Reputation
@CodeSlave: I suppose it looks nicer if you're embedding html code.Heteronomy
Q
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.

Quaker answered 7/5, 2010 at 13:45 Comment(0)
H
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.

Heaver answered 7/5, 2010 at 13:33 Comment(0)
S
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;
Singletree answered 7/5, 2010 at 13:38 Comment(0)
N
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.

Notum answered 7/5, 2010 at 13:41 Comment(1)
This syntax can be very useful in templates, hence ihmo not questionable. Or at least not obviously;)Passade

© 2022 - 2024 — McMap. All rights reserved.