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 = ($apple !=”green”) ? “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
Potato / Pototo
Why is it so terrible? Care to explain?
only thing i’d change is the lack of curly brackets. lol. great article!
I’ve heard this alternative syntax will be ended up in the new version of the PHP, the 6.0. Could someone confirm it or not in definitely, please? Cheers.
I don’t think so, because a lot of widely used Open source software use the alternative syntax, what *might* be discarded would be the < ?= ?> type syntax. You can follow the thread on sitepoint here http://www.sitepoint.com/forums/php-34/no-alternative-syntax-php-6-a-616859.html and check in the list of changes drafted for PHP 6 here http://wiki.php.net/todo/php60
Handy page, very clearly laid out, and I was only aware of the {} syntax myself till I read this.
I’d have to agree that the : method seems harder to read but still very simple and straight forward.
You might be testing readership with the “IF Statement in one line” section but there’s two typos you may want to fix for the OCD types:
“3 print $eatit = ($applet !=”gree”) ? “Eat” : “Dont Eat”;”
should be
“3 print $eatit = ($apple !=”green”) ? “Eat” : “Dont Eat”;”
Cheers!
Thanks Metrofax
for pointing out the typos, glad you enjoyed the article!
You shouldn’t be advising people to use it, it’s pretty much the opposite of all the things you said it is.
It’s ugly.
It’s hard to read.
It’s hard to debug.
The ternary operator is okay, but normally in if statement is better due to readability.
That’s just my opinion, probably the same as many others I’d say.
Jason,
To each his own?
I love the : syntax, It’s more clear to me than using { }, I love {} too. It might be hard to debug I will give you that, but how is it that its hard to read and ugly? Ternary operators would save you a lot bytes if you have a lot of “if”s in the code.
JMPO.
using the if (…) :
endif;
syntax is great if you have blocks of html code in between your php. I find it’s easier to scan for endif; or endwhile; when there is mixed html and php rather than searching through code looking for that missing curly brace
Agreed!
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.
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 don’t 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. 
This is a terrible practice, and no one should be using this. The shorthand If/Else statement is just about the only thing that is OKAY on this page.