views:

127

answers:

8

Hi guys.

When working with open source project (like wordpress, drupal, joomla) i always find in the php pages a syntax like (this is an example from drupal):

<?php if ($linked_site_logo or $linked_site_name): ?>
  <?php if ($title): ?>
    <div class="logo-site-name"><strong>
      <?php if ($linked_site_logo): ?><span id="logo"><?php print $linked_site_logo; ?></span><?php endif; ?>
      <?php if ($linked_site_name): ?><span id="site-name"><?php print $linked_site_name; ?></span><?php endif; ?>
    </strong></div>           
  <?php else: /* Use h1 when the content title is empty */ ?>     
    <h1 class="logo-site-name">
      <?php if ($linked_site_logo): ?><span id="logo"><?php print $linked_site_logo; ?></span><?php endif; ?>
      <?php if ($linked_site_name): ?><span id="site-name"><?php print $linked_site_name; ?></span><?php endif; ?>
   </h1>
  <?php endif; ?>
<?php endif; ?>

while i do use a different syntax writing my scripts; if i did wrote the previous example it would look something like:

<?php
if($linked_site_logo or $linked_site_name){
    if($title){
        echo '<div class="logo-site-name"><strong>';
        if($linked_site_logo){ echo '<span id="logo">' . $linked_site_logo . '</span>'; }
        if($linked_site_name){ echo '<span id="site-name">' . $linked_site_name . '</span>'; }
        echo '</strong></div>';
    }else{ /* Use h1 when the content title is empty */
        echo '<h1 class="logo-site-name">';
        if($linked_site_logo){ echo '<span id="logo">' . $linked_site_logo . '</span>'; }
        if($linked_site_name){ echo '<span id="site-name">' . $linked_site_name . '</span>'; }
        echo '</h1>';
    }
}
?>

Now, lets skip the 'appareance' of the 2 syntax methods, becose it is maybe a matter of taste and/or custom (obviously I prefer the second method), the question is:

Does the first syntax (breakinf the 'if' statements, output the html instead of echo it, have a lot of php snippets even if they arent really needed) have some technical advantages over the second one? (for example the script run faster, is easier to debug, etc...) Or is just a open source programmers unwrited convention?

+4  A: 

It's all about readability.

I don't know what you mean by output vs echo. There is no difference. They're just different ways of printing "stuff" to output that is sent to the client.

The disadvantage of:

echo "<div id=\"blah\">";

is twofold:

  1. The extra slashes require effort to put in and make it less readable; and
  2. HTML outside PHP code blocks will syntax highlighted by most PHP editors.
cletus
...and the first one is considered more readability? Then, as i suppose is a matter of taste ;)
DaNieL
Well take your question as a case in point: the first version has the HTML syntax highlighted. The echo version doesn't.
cletus
Yes but, having long and wired 'if' statements will have another trouble: in the first method, the only way you have to see wheere start and close an if is by the indentation, instead using the second method you'll have the {} highlighted with every php editor
DaNieL
I think you'll find a decent PHP editor will do that for the first case too.
cletus
I use braces in templates too. The only editor that I know of which is capable of highlighting the alternative syntax is TextMate through some plugin.
Ionuț G. Stan
I use Komodo edit... and i dont think is indecent, but doesnt hihligh the 'alternative' endif; syntax ;)
DaNieL
+2  A: 

The biggest "advantage" I could see to the former method would be that it's easier to insert HTML anywhere within the overall control flow - if you wanted to output some HTML before the if($title) check, you could just insert a line above it with the HTML, no need to escape things for an echo or whatnot.

Amber
+4  A: 

I wouldn't go as far as saying echoing HTML is evil in all cases, but it certainly has a lot of drawbacks. In addition to what cletus points out, your HTML is not structured anymore, i.e. the indention levels give you no indication of where you are in the document structure. That's a biggie for me.

Personally, I dislike the first style as well, as it makes the PHP code harder to read. I always try to strike a balance, multi-line PHP statements belong in one <?php ?> block, but HTML always belongs outside the <?php ?> block. In edge cases, e.g. when indention levels change inside the PHP block, I tend towards closing it and beginning a new block.

I can see that that opens up a can of worms regarding edge cases and when to use which, so I'm sympathetic to open source projects setting a formal rule to always close <?php ?> blocks.

deceze
this is right. readability is not just for the PHP code, its also about the HTML output and how easy it is to debug it. using the 2nd method generates cultured and non-uniform code (unless you are very precise on using \n`s and \t`s)
XiroX
**@XiroX:** Actually, I'm talking about the source code, not the HTML output. Getting 100% correctly indented HTML output is almost an impossibility for non-trivial scripts. Just think automatic `array() -> <table>` conversion functions for example. And the HTML output doesn't need to be formatted correctly, **it just needs to be valid and machine readable**. I rarely need to look at it directly, Firebug or the Webkit Web Inspector are magnitudes better for examining HTML output.
deceze
A: 

They are the same. I suggest you stick what you have been used to do because that is more readable to you.

ian
+1  A: 

afaik The reason for this is that graphic designers can edit the HTML in their tools (dreamweaver and similar). Those tools would show the php tags as just that or even hide them completely. That way they can design away without touching your code, which is, believe me, a massive advantage when collaborating with designers.

handsomeGun
A: 

If you follow MVC - you have the view and model (domain logic) parts. For the view you use the first method because it's HTML with tiny PHP parts in it, and for the model you use the second method - it's pure PHP anyway. It's a very common approach afaik.

Examples:

Zend Framework - see zend view manual

WordPress - the code (even messy parts) are method 2, and the themes are method 1

disjunction
+1  A: 

Actually they are not the same. in fact in your second example, php interpreter will do unnecessary step, which is printing out html elements. thus resulting poor performance depending on the size of the page. checout google's article "lets make web faster" http://code.google.com/speed/articles/optimizing-php.html.

Mohamed
Interesting.. for 'unnecessary steps' do you mean the echo of the solo html tags, like echo '<div class="logo-site-name"><strong>'; ?This issue is been the first that i thought about looking at the first syntax, but im wondering how much will it slow the performance.. anyway, even if the loss of performance is little, why not avoid it?
DaNieL
yes, but the performance will depend on the page size.besides the second version is ugly and no proper separation between PHP and Html.
Mohamed
A: 

Keeping one hierarchy of consistent indentation for both code and markup is essential for coping with complex templates. In the first example I can immediately see the structure of the tags; the second makes me work to understand what's going on. Without reading through it I can't see whether it's doing something like leaving an element open. IMO PHP should be written like XHTML, as if the ‘if’ statements were tags you had to balance.

(Personally though I prefer the standard {...} syntax to the alternative :...endif one. I don't see what advantage that brings.)

Legend has it that direct PHP-templated output is marginally faster than echoing everything. But if there's really any difference it's too small for me to measure. Certainly compared to any other work your script will be doing, it's inconsequential. t's only the readability that really matters. PHP is a templating language, though — you might as well take advantage of it!

[both examples fail to htmlspecialchars, tsk.]

bobince