PHP One Line IF Statement

I'm sure if you've been programming for a few years that you've come across when you need to check if something equals something than do something else a few times.

Well the best way to do this is like so.

Say for example we have a variable called $num and the value of it is 5.

You could store it in a variable.

$code = ($num > 0) ? "More than 0" : "Less than 0";

or you could print it straight to the users browsers

echo ($num > 0) ? "More than 0": "Less than 0";

So what happens here is if $num is greater than 0 print More than 0 else print Less than 0.

I hope you've found this quick tip helpful and use it throughout future projects.

Bookmark and Share
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • Facebook
  • Mixx
  • Google Bookmarks
  • email
  • FriendFeed
  • LinkedIn
  • Tumblr
  • Twitter

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

, , , ,

6 Responses to PHP One Line IF Statement

  1. za3lane September 15, 2008 at 4:50 pm #

    helo , sorry about this but your tutorials need upgrading because you have not the technics to explain to other .
    yu just write that is all ,
    you should get a technic to explain to other , therfor ill show you awebsite to learn from the technic however it talks arabic , just take alook and see how

  2. c.b. October 7, 2008 at 11:49 am #

    I'm new to PHP and recently setup my local machine with PHP and MySQL for doing development. I was sort of stuck when I needed to post my work for the user to test and review. After looking around a bit I found a site that hosts PHP and MySQL apps. I was surprised that it was free - it seems they're offering the service at no cost until 2012. At that point they'll change over to a fee-based service. However, in the meantime, it's a great place to do anything from demo and sandbox right up to posting sites for real.

    Their pitch is as follows:

    "This is absolutely free, there is no catch. You get 350 MB of disk space and 100 GB bandwidth. They also have cPanel control panel which is amazing and easy to use website builder. Moreover, there is no any kind of advertising on your pages."

    Check it out using this link:

    Thanks and good luck!

  3. THE|ODIN October 9, 2008 at 6:42 am #

    Wicked Tutorial. I must have looked this up a hundred times, great reference guide, pointless fact but this is in fact known as a ternary statement, Thankyou!

  4. jnthn December 9, 2008 at 12:28 pm #

    As THE|ODIN Says, this is a ternary operator. It's really a one-line if-then-else statement. If TRUE then do what follows the "?" else do what follows the ":"

    Have you ever seen something like:

    "You have 1 items in your shopping cart."

    For those who care, this sentence is grammatically incorrect. The simplest way to avoid this problem is:

    ($cart_Count == 1) ? "You have 1 item in your shopping cart.": "You have $cart_Count items in your shopping cart.";

  5. jason December 22, 2009 at 2:07 am #

    it took me while to figure out the problem with "print it straight to users browsers" and it's that you left out "echo" in the beginning.

    i.e. echo ($num > 0) ? "More than 0": "Less than 0";

  6. Wez Pyke December 22, 2009 at 12:11 pm #

    Sorry about that, I have modified the post so future readers will not have the same problem.

Leave a Reply