views:

8803

answers:

10

Are there any differences between these?

if ($value) {

}

if ($value):

endif;

I think the second way is new to PHP 5.. therefore is it better because it is newer?

Update

I have been informed that the second way is not new.

+12  A: 

They are the same but the second one is great if you have MVC in your code and don't want to have a lot of echos in your code. F.e. in my .phtml files (Zend Framework) I will write something like this:

<?php if($this->value): ?>
Hello
<?php elseif($this->asd): ?>
Your name is: <?= $this->name ?>
<? else: ?>
You don't have a name.
<?php endif; ?>
Thomaschaaf
Won't that work with curly brackets too?
alex
@ alex - i think so. however if you google stringtemplate (all one word) you'll find that they use a very similar syntax to solve this same problem. I guess you could argue the code is cleaner looking? Maybe this is common for interpreted templating/languages...
sweeney
@alex It will work with curly brackets as well, but at least personally I find this way kind of clearer in things like this. Cause they you know that it is the end of an if, and not the end of a loop of some sort or something else. Think you have endfor and endwhile or something similar too.
Svish
I wouldn't use the <?= ?> because it is not supported on all servers, especially shared hosting ones that don't allow you to change php.ini.
Exception
+3  A: 

I would use the first option if at all possible, regardless of the new option. The syntax is standard and everyone knows it. It's also backwards compatible.

Paulo
+3  A: 

At out company, the preferred way for handling HTML is:

<? if($condition) { ?>
   HTML content here
<? } else { ?>
   Other HTML content here
<? } ?>

In the end, it really is a matter of choosing one and sticking with it.

gahooa
Sorry, it was a bad example. I have updated it.
alex
Thanks -- I updated this answer. It beats around the bush a little, but may help out.
gahooa
Who do you mean when you say "our"?
Oddthinking
@Oddthinking: The company he works for I assume?
alex
+1 for avoiding excessive echo statements and escaped quote marks. :)
John McCollum
+5  A: 

Here's where you can find it in the official documentation: PHP: Alternative syntax for control structures

yjerem
+2  A: 

Both are the same.

But: If you want to use PHP as your templating language in your view files(the V of MVC) you can use this alternate syntax to distinguish between php code written to implement business-logic (Controller and Model parts of MVC) and gui-logic. Of course it is not mandatory and you can use what ever syntax you like.

ZF uses that approach.

Itay Moav
+1  A: 

There is no technical difference between the two syntaxes. The alternative syntax is not new; it was supported at least as far back as PHP 4, and perhaps even earlier.

You might prefer the alternative form because it explicitly states which control structure is ending: endwhile, for example, can only terminate a while block, whereas if you encounter a brace, it could be closing anything.

You might prefer the traditional syntax, though, if you use an editor that has special support for braces in other C-like syntaxes. Vim, for example, supports several keystrokes for navigating to matching braces and to the starts and ends of brace-delimited blocks. The alternative syntax would break that editor feature.

Rob Kennedy
A: 

I think it's a matter of preference. I personally use:

if($something){
       $execute_something;
}
Sergio Rodriguez
if you'd read the whole thread: alternative syntax is preffered and much <em>cleaner</em> when templating or using foreign language in php views
Juraj Blahunka
+10  A: 

I personally really hate the alternate syntax. One nice thing about the braces is that most IDEs, vim, etc all have bracket highlighting. In my text editor I can double click a brace and it will highlight the whole chunk so I can see where it ends and begins very easily.

I don't know of a single editor that can highlight endif, endforeach, etc.

dprevite
This is supposed to be used in PHP templates, where beginning and end of block are separated by chunk of foreign language. If your IDE understands PHP well enough to parse that, it should also highlight `endif` as end-of-block properly. If it doesn't, you don't really buy anything by using `}` as it won't match anyway.
Pavel Minaev
+3  A: 

I think this say it all:

this alternative syntax is excellent for improving legibility (for both PHP and HTML!) in situations where you have a mix of them.

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

When mixing HTML an PHP the alternative sytnax is much easier to read. In normal PHP documents the traditional syntax should be used.

+1, Nice answer on what its purpose is. Personally I think it's a pointless addition to an already cluttered language.
Draemon
+1  A: 

I used to use the curly braces but now a days I prefer to use this short-hand alternative syntax because of code readability and accessibility.

I use it in my view templates.
alex