Приглашаем посетить
Чернышевский (chernyshevskiy.lit-info.ru)

Avoiding Confusing Code

Previous
Table of Contents
Next

Avoiding Confusing Code

In a way, everything discussed so far in this chapter falls into the category "avoiding confusing code." Following a particular code style is a way of making all the code in a project look the same so that when a new developer looks at the code, the logic is clear and no style barriers need to be overcome. General rules for layout and naming aside, there are some additional steps you can take to avoid code that is obtuse. They are described in the following sections.

Avoiding Using Short Open Tags

PHP allows the use of so-called short tags, like this:

<?
print "Hello $username";
?>

However, you should never use them. Parsing short tags makes it impossible to print normal XML documents inline because PHP would interpret this header as a block and will attempt to execute it:

<?xml version="1.0" ?>

You should instead use long tags, as in this example:

<?php
print "Hello $username";
? >

Avoiding Using echo to Construct HTML

One of the principal beauties of PHP is that it allows for embedding of HTML in PHP and PHP in HTML. You should take advantage of this ability.

Take a look at the following code snippet that constructs a table:

Hello <?= $username ?>
<?php
echo "<table>";
echo "<tr><td>Name</td><td>Position</td></tr>";
foreach ($employees as $employee) {
  echo "<tr><td>$employee[name]</td><td>$employee[position]</td></tr>";
}
echo "</table>";
?>

Compare this with the following:

<table>
  <tr><td>Name</td><td>Position</td></tr>
<?php foreach ($employees as $employee) { ?>
  <tr><td><?php echo $employee['name'] ?></td><td><? echo $employee['position']
?></td></tr>
<?php } ?>
</table>

The second code fragment is cleaner and does not obfuscate the HTML by unnecessarily using echo. As a note, using the <?= ?> syntax, which is identical to <?php echo ?>, requires the use of short_tags, which there are good reasons to avoid.

print Versus echo

print and echo are aliases for each other; that is, internal to the engine, they are indistinguishable. You should pick one and use it consistently to make your code easier to read.


Using Parentheses Judiciously

You should use parentheses to add clarity to code. You can write this:

if($month == 'february') {
  if($year % 4 == 0&& $year % 100 || $year % 400 == 0) {
    $days_in_month = 29;
  }
  else {
    $days_in_month = 28;
  }
}

However, this forces the reader to remember the order of operator precedence in order to follow how the expression is computed. In the following example, parentheses are used to visually reinforce operator precedence so that the logic is easy to follow:

if($month == 'february') {
  if((($year % 4 == 0)&& ($year % 100)) || ($year % 400 == 0)) {
    $days_in_month = 29;
  }
  else {
    $days_in_month = 28;
  }
}

You should not go overboard with parentheses, however. Consider this example:

if($month == 'february') {

  if(((($year % 4) == 0 )&& (($year % 100) != 0)) || (($year % 400) == 0 )) {
    $days_in_month = 29;
  }
  else {
    $days_in_month = 28;
  }
}

This expression is overburdened with parentheses, and it is just as difficult to decipher the intention of the code as is the example that relies on operator precedence alone.


Previous
Table of Contents
Next