Your Ad Here

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

($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.

3 Responses to “PHP One Line IF Statement”

  1. za3lane Says:

    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. Says:

    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:

    http://www.000webhost.com/83188.html

    Thanks and good luck!

  3. THE|ODIN Says:

    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!

Leave a Reply

Your Ad Here