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.
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?
I have been informed that the second way is not new.
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; ?>
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.
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.
Here's where you can find it in the official documentation: PHP: Alternative syntax for control structures
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.
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.
I think it's a matter of preference. I personally use:
if($something){
$execute_something;
}
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.
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.
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.