Skip to Content

Devin Clark

Alternative Syntax in PHP, FTW!

This is a guest post by my co-worker and friend, the wonderful Chris Ensell. Chris is a Freelance Web Developer and is my go-to guy for any PHP questions I have (and I have a lot).

For newer coders, browsing through code can be daunting. There can be a LOT of brackets to go through, and it gets easy to get lost. Especially if you aren't the one who wrote it. Sometimes, even experts can have problems, especially when troubleshooting.

There is one way of writing code that will help you, and others who may use your code after you. Alternative syntax, and since I have started using it, I have fallen in love.

Let's start with a little code.

function getTags( $text ) {
  $posttags = get_the_tags();
  if ( $posttags ) {
    foreach( $posttags as $tag ) {
      $theTag = $tag->slug;
      if ( !$theTag == "foobar" ) {
        $url = get_bloginfo('url');
        foreach ( $starts as $start ) {
          $text = str_replace($start.$theTag, '<
          a href="'.$url.'/?tag='.$theTag.'">' . $theTag . '', $text);
        }
      } else {
        $text = str_replace($start.$theTag, '<strong>'.$theTag.'</strong>', $text);
      }
    }
  }
  return $text;
}

This is a part of one of my plugins I am working on that searches posts for the names of tags you're blog uses and links them to the proper archive page. Granted, I padded the code up a bit with some useless stuff and reverted it back to the old school syntax.

So, what can we do to make this more readable, especially to newer coders? Alternative syntax is a perfect starting point. So, let me show you a few examples of that...

This is a basic else/if statement, using the old classic syntax.

if ( $foo ) {
  $bar = false;
} elseif ( $bar ) {
  $foo = false;
} else {
  $foobar = "howdy!";
}

First alternative syntax I'll show you can only be used if you are using it to define a variable, or something that only needs one line.

if ( $foo )
  $bar = false;
elseif ( $bar )
  $foo = false;
else
  $foobar = "howdy!";

As you can see, the brackets are completely gone now. This is great if its a simple elseif. You can also put it all on one line for easier organization:

if ( $foo )
  $bar = false;
elseif ( $bar )
  $foo = false;
else $foobar = "howdy!";

As you can see, it makes things easier to navigate.

Now, if you have multiple lines, you are going to want to use this method:

if ( $foo ) :
  $bar = false;
elseif ( $bar ) :
  $foo = false;
else :
  $foobar = "howdy!";
endif;

What we did here is pretty much replace the brackets with colons and at the end replaced the final bracket with an endif.

So, let's put it to use on that original example I posted at the beginning.

function getTags( $text ) {
  $posttags = get_the_tags();
  if ( $posttags ) :
    foreach( $posttags as $tag ) :
      $theTag = $tag->slug;
      if ( !$theTag == "foobar" ) :
        $url = get_bloginfo('url');
        foreach ( $starts as $start )
          $text = str_replace($start.$theTag, '<a href="'.$url.'/?tag='.$theTag.'">' . $theTag . '', $text);
      else :
        $text = str_replace($start.$theTag, '<strong>'.$theTag.'</strong>', $text);
      endif;
    endforeach;
  endif;
  return $text;
}

See! Isn't that cleaner looking? You will also find it easier to find where one statement ends more easily. This is an excellent way to code. You will find that WordPress uses this a lot in their themes as well, and for good reason.

This doesn't only apply to elseif. It also works with while, for, foreach, and switch.

You can find more information in the PHP manual.

Photo of Devin Clark
Devin Clark
Principal Software Engineer, Oracle
You Might Also Like