Jump to content

Alternate syntax of PHP

- - - - -

  • Please log in to reply
5 replies to this topic

#1
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
A great thing about PHP is that it allows you to use alternate syntax on specific statements. We can utilize this to create clearer looking code as our brain often uses indentation for separation faster than matching braces up.

To show you what I mean, take a look at the following three structures. If, Else, and While.
while (1) {
  if(someInput() == true) {
    doSomething($someVar);
  } else {
    panic();
  }
}
You may be already looking at the IF, and eye up to the WHILE to see where the statements belong.

One may dare to use comments, which can be especially handy for structures that span multiple pages:

while (1) {
  if(someInput() == true) {
    doSomething($someVar);
  } else {
    panic();
  } //End IF
} // End While
Well, excessive comments are what we constantly fight to keep out of our code!

Here we introduce the alternative syntax:

while(1):
  if(someinput() == true):
    doSomething($someVar);
  else:
    panic();
  endif;
endwhile;
As you can see, braces are replaced by colons and end statements, with a more meaningful set of symbols to them.

The following can be safely used as alternates:

While:
while(1):
  statement;
endwhile;
If, If/else/elseif:
if(1):
  statement;
else: 
  statement;
endif;
For:
for($i = 1; $i <= 20; $i++):
  statement;
endfor;
Foreach:
foreach($apples as $apple):
  statement;
endforeach;
Switch:
switch($days):
  case 1: statement; break;
endswitch;
And I had almost forgotten to mention, there lays an option called open short tags which allows for this syntax, which can be handy when writing a view for the data retrieved from a model:
<?= $blogtitle ?> (is the same as) <?php echo $blogtitle; ?>
A word of caution though, this exact feature may be turned off by default in some installations, so ensure the PHP directive short_open_tag is set to 1 before using this style.

Edited by Alexander, 24 February 2011 - 06:06 PM.
Info about open short tags

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#2
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 765 posts
So basically you can replace { with : and } with end + whatever command that starts the block? Very nice info here.

#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
I had just not seen this used much, and some think it is not right so I thought I would write something about it, to show it has uses. Ironically after I wrote this, I noticed John had a blog entry about it a few years back http://forum.codecal...lon-syntax.html
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#4
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 765 posts
^ Lol! Me actually never seen this way of php coding. But I learn from reading other's code, not from text books, trainings, or official tutorials. So forgive me, :c-grin:

The most thing I hate from php is when you mix html codes inside php codes. Really make my head spins. So what if we are using this "dialect"? do you think it will reduce my headache (making the codes much more easier to comprehend)? I have to ask, for I dont have sample codes using this "dialect" . :)

#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
It looks fairly nice when decoupling logic from view (no HTML codes echo'd!), an example of what most frameworks would teach to make a view of a blog:
<html>
  <head>
    <title><?= $blogtitle ?></title>
  </head>
  <body>
    <?php foreach($blogitem as $entry): ?>
      <h1><?= $entry->heading ?></h1>
      <p>
        <?= $entry->body ?>
      </p> <hr/>
      <div>
        <?= "Post by: " . $entry->author . " at " . $entry->postdate ?>
      </div>
    <?php endforeach; ?>
    </body>
</html>
It looks fairly clean, a lot better than foreach's braces and ugly printing everywhere, it can get a bit tiring on the eyes though so I save it for just the special occasions.

Edit: I forgot to mentioned the <?= syntax as well, silly me!
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#6
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 765 posts
Thanks for the example! Completely agree this approach looks a lot neater that the "conventional" way. Why little realize that the <?= tag produce easier to read codes?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users