Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Alternate syntax of PHP

syntax

  • Please log in to reply
5 replies to this topic

#1 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 22 February 2011 - 11:29 PM

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

  • 1

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#2 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 23 February 2011 - 12:36 AM

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

#3 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 23 February 2011 - 01:55 AM

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
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#4 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 24 February 2011 - 05:35 AM

^ 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" . :)
  • 0

#5 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 24 February 2011 - 05:50 PM

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!
  • 1

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#6 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 03 March 2011 - 02:28 AM

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?
  • 0





Also tagged with one or more of these keywords: syntax

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download