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 = ($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!

Post Rating 3.00 out of 5


Subscrive via feedburner rss

Subscrive via feedburner email
3 Responses to “PHP Shorthand and Alternate Syntax: A quickie!”
Comment History Slider
    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 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

Deliverables

  • Website design/interface layout
  • CMS Theme-ing
  • Website/webapp/RIA Development
  • Website redesign
  • Webmaster services
  • Consultations on marketing/branding/etc over Coffee/beer/lunch/dinner

My Value Proposition

  • Fast turn around times.
  • Free consultations on marketing and branding
  • Affordable services
  • Around the clock support.
  • An experience of over 14 years with providing these services.

My services for barter.

  • If you think you might have something that I would use/want, I am ready to provide my services for $1.00 and a barter!

Eventify – Simple Events

NextGEN Resize Wordpress Plugin

Categories

Recent Posts

Looking for something?

Thanks to Subscribers

Feedburner : 10


Twitter : 1992

Thank you for taking the time out.

Your Email (required)

Your Email (required)

Your Feedback

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.