Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opensource project's PHP syntax

Tags:

syntax

php

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?

like image 321
Strae Avatar asked Aug 07 '26 20:08

Strae


1 Answers

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.
like image 62
cletus Avatar answered Aug 10 '26 08:08

cletus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!