Opensource PHP syntax

When working with an open source project (for example, wordpress, drupal, joomla), I always find in PHP pages 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 use a different syntax for writing my scripts; if I wrote the previous example, it would look something like this:

<?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 let's skip the "game" of the two syntactic methods, because it might be a matter of taste and / or custom (obviously I prefer the second method), the question is:

Does the first syntax (breakinf into if statements, output HTML instead of echo it), have a lot of PHP snippets even if they are really needed) have some technical advantages over the second? (e.g. script is faster, easier to debug, etc.) Or just open source developers with no record?

+2


source to share


8 answers


It's all about readability.

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

Disadvantage:



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

      

has a twofold meaning:

  • Extra slashes take effort to insert and make them less readable; and
  • HTML outside PHP code blocks will be the syntax highlighted by most PHP editors.
+4


source


I wouldn't say echoing

HTML is evil in all cases, but it certainly has many flaws. In addition to what cletus points out , your HTML is no longer structured, meaning the indentation levels give you no indication of where you are in the document structure. This is great for me.

I personally don't like the first style either, as it makes PHP code more difficult to read. I always try to balance, multi-line PHP statements belong to the same block <?php ?>

, but HTML always belongs outside the block <?php ?>

. In edge cases, for example. when the indentation levels change inside a PHP block, I try to close it and start a new block.



I see this opens up a can of worms about the edges and when to use, so I sympathize with open source projects setting a formal rule to always close blocks <?php ?>

.

+4


source


The biggest "advantage" I could see in the first method would be that it is easier to insert HTML anywhere in the overall control flow - if you want to output some HTML before checking if ($ title), you can simply insert a line above with HTML, there is no need to avoid things for echoing or whatever.

+2


source


afaik. The reason is that graphic designers can edit HTML in their tools (Dreamweaver and similar). These tools would show php tags as simple or even hide them entirely. This way they can be designed without touching your code, which is a huge advantage when working with designers.

+1


source


They don't really match. in fact, in your second example, the php interpreter will do the unnecessary step that prints the html elements. which leads to poor performance depending on the page size. checout google article "makes the web faster" http://code.google.com/speed/articles/optimizing-php.html .

+1


source


They are the same. I suggest you stick with what you used because it is more readable for you.

0


source


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

Examples:

Zend Framework - see zend viewer guide

WordPress - Code (even the dirty parts) is Method 2 and Themes is Method 1

0


source


Keeping the same hierarchy of consistent indentation for code and markup is essential for solving complex patterns. In the first example, I can immediately see the structure of the tags; the second makes me work to understand what is going on. Without reading this, I cannot see that it is doing something like leaving the element open. IMO PHP should be written as XHTML, as if the if statements were tags you were supposed to balance out.

(Personally, I prefer the standard syntax {

... }

for the alternative :

... endif

I don't see the benefit.)

Legend has it that direct PHP templating output is slightly faster than echoing. But if there is any difference, it is too little for me. Of course, compared to any other work your script will do, this is immaterial. t just the readability that really matters. PHP is a templating language, but you can use it!

[both examples do not match htmlspecialchars, tsk.]

0


source







All Articles