PHP Shorthand and Alternate Syntax: A quickie!
Ok, the title pun, is intended.
When I started out with PHP, every project I worked on required me to keep typing incessantly, not that I don’t like my keyboard or the keys on it, but sometimes the syntax does get to ya and not to mention the missing/dangling braces!
I reached a point where I needed a PHP IDE, [yes I was using plain text editor at that time], to help me with preserving my keyboard and my sanity. This is the time I also stumbled on some PHP shorthand techniques and the alternative syntax. I was reminded of this when my assistant, was breaking away at my precious keyboard. So I thought I did ask him to learn some.
And thats how this post came into being.
Some PHP alternative syntax and shorthand techniques.
IF-ELSE Conditional Construct.
1 if(condition) :
2
3 //statements
4
5 endif;
IF-ELSEIF-ELSE
1 if(condition) :
2
3 //statments
4
5 elseif(condition) :
6
7 //statments
8
9 elseif(condition) :
10
11 //statments
12
13 endif;
IF Statement in one line
1 $apple = "green";
2
3 print $eatit = ($applet !="gree") ? "Eat" : "Dont Eat";
SWITCH
1 switch($var):
2 case 1:
3 //statements
4 break;
5
6 case 2:
7 //statements
8 break;
9 case 3:
10 //statements
11 break;
12 endswitch;
WHILE
1 while(condition):
2 //statements;
3 endwhile
FOR
1 for(exp):
2 //statements;
3 endfor;
FOREACH
1 foreach(exp):
2 //statements;
3 endforeach;
ECHO
1. <?= $var; ?>
is same as
1. echo $var;
Why should you use PHP shorthand / Alternative Syntax?
1. Saves time with the ternary operators used in the if statement.
2. Saves your sanity, if you are writing 100s of lines of code, alternative syntax does help with the braces, ALOT!
3. Great to understand and debug.
4. End makes it easier to know which control structure is ending as compared to counting braces.
5. Small Code.
If you know any more short cuts or have suggestions, corrections please do help! Comment and let me know!
More Examples and Reading
1. PHP Documetation on Alternative Syntax2. David Walsh's - PHP Shorthand If / Else Examples
Tags: if and else shorthand, PHP, php alternative syntax. php shorthand, php for alternative syntax, php foreach alternative syntax, php syntax, php while alternative syntax
Yeah, thanks for the heads up the short tags.. Here is more on the topic of deprecating shot tags and its really sad that I would have to sit and change all my code replacing short tags to get it up to speed with PHP 6. They could have done better and worse.
And yea, I hate {}, I guess its my eyes ![]()
on a serious note when you are developing on PHP all day long counting braces even with editors highlighting it, it does take time. I find using alternative sytax much better, to each his own!
thanks for your comment!














Every website I design/develop, I follow some essential steps to make your time and investment count. From free consultation to free post-production support. 
Unfortunately I think they are deprecating short tags, so that one is out.
As for the alternative syntax, I personally don’t like it because it forces you to look for the end tag instead of having your editor find it and highlight it {} (most editors that I have tried anyway). The syntax also reminds me of smarty…. which I hate. To each his own I guess.