Feedback

About Designerfoo and Me!

I am Manoj Sachwani, a webmaster, wannabe illustrator, designer, developer, amongst other things. I have over 14 years of experience, developing, designing and deploying websites. I am geeky, hairy and and I love what I do.

I stumbled & bought this domain - Designerfoo.com - because I thought it would be a "cool" domain to have, as time went on, I realized that I want to become - "Designerfoo" - and grow my knowledge in design, illustration, web design and web development.

I am webmaster, I design and develop websites with an edge. How may I help you?
Contact   -   @designerfoo   -   Facebook
Who is

PHP Shorthand and Alternate Syntax: A quickie!

DesignerfooDevelopment + PHP + Tutorial • 10-01
Comment(s)

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!

Post Rating 3.00 out of 5


Subscrive via feedburner rss

Subscrive via feedburner email
15 Responses to “PHP Shorthand and Alternate Syntax: A quickie!”
Comment History Slider
    January 29, 2012 at 2:02 am
    Author: Adam Fowler

    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.

      April 10, 2012 at 1:29 pm
      Author: Designerfoo

      Potato / Pototo :) Why is it so terrible? Care to explain?

    October 15, 2011 at 2:07 pm
    Author: kevin

    only thing i’d change is the lack of curly brackets. lol. great article!

    March 2, 2011 at 1:29 am
    Author: medina

    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.

    January 21, 2011 at 11:37 pm
    Author: MetroFax

    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!

      January 22, 2011 at 6:37 am
      Author: Designerfoo

      Thanks Metrofax :) for pointing out the typos, glad you enjoyed the article!

    July 12, 2010 at 2:19 pm
    Author: Jason

    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.

      July 13, 2010 at 5:31 am
      Author: Designerfoo

      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.

      July 20, 2010 at 6:17 am
      Author: Aaron

      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 ;)

        July 20, 2010 at 7:28 am
        Author: Designerfoo

        Agreed! :)

    January 22, 2010 at 10:00 pm
    Author: SeanJA

    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.

      January 23, 2010 at 9:41 am
      Author: Designerfoo

      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 :D
      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!



Leave a reply :)

Name:

(required)

Mail (will not be published):

(required)

Website:

Comment:


Subscribe

Links

Eventify – Simple Events

NextGEN Resize Wordpress Plugin

Categories

Recent Posts

Looking for something?

Thanks to Subscribers


Warning: gzinflate() [function.gzinflate]: data error in /home2/enyecons/public_html/designerfoo.com/wp-includes/http.php on line 1787
  • oops .. the whale has sunk. Twitter Status: Down!

Thank you for taking the time out.

Your Email (required)

Your Email (required)

Your Feedback

Captcha
captcha

What to do you think of Eventify - Simple Events?

View Results

Loading ... Loading ...

What do you think about the NextGEN Resize Plugin?

View Results

Loading ... Loading ...

What do you think about the site design/UI/IA?

View Results

Loading ... Loading ...

  X Close Feedback.