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 WhileWell, 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


Sign In
Create Account

Back to top










