Short If Statement in PHP

PHP – One Line If Statement – Short If Statement – Condensed If Statement

I always have to look this up, so here it is for easy reference. Sometimes is makes the code look less cluttered if you can express an ‘”if” in one line.

The one line if statement is also known as the ternary operator if you want to look it up in the PHP documentation. It has also been called the short if statement and the one line if statement.

//Assign a string value to $x
$x = ($myvalue == 10) ? "the value is 10": "the value is not 10";

Let’s say you decide to print the value of $x after the above statement has run.
If the value of the variable $myvalue is equal to 10, the string “the value is 10” will be printed out. But if the value of $myvalue is anything other than 10, the string “the value is not 10” will be printed out.

No related posts.

Comments

  1. Andrew Y says:

    Good I needed to know what this meant thanks :-)

  2. Adrien says:

    Thanks!I was looking for this since a while.Really useful.

  3. Duncan says:

    As you said Liz a little tired late at night, did a little comprehensive version of my own, even the online PHP manual is a little thin on this topic:
    http://webtechmech.com/2009/11/short-php-if-statements.html

  4. Dean says:

    Not quite… if $myvalue is 10 then x string “the value is 10″ will be placed in the variable $x.

  5. forgot it again and now looked again in google and it brought me back here lol, just like Crazy (…person who commented 1st)

  6. Alaa hamoud says:

    I need more information about Statement in PHP

  7. matt says:

    cheers been lucking for this for 15min now in google.

  8. Liz Jamieson says:

    Oh! I know I do most of this stuff in the middle of the night, and I am a zombie during daylight hours, but I thought that IS what I said. I’m not sure what you’re trying to tell me.

  9. Tomas says:

    Thanks for the snippet. I think, however, that you ment if the value of $myvalue is equal to 10 than set the value of $x to “the value is 10″. Otherwise, great!

  10. Jake says:

    cool – am always forgetting this and spending ages looking up. Will book mark this and hopefully remember. I need it for perl, but it is all the same.

  11. Charlie S says:

    Useful and quick find, thanks much.

  12. website bouwers apeldoorn says:

    Nice one, just what i’m looking for!

  13. Liz Jamieson says:

    I’ve always wanted someone to tell me that. Thank you!

  14. Mike says:

    You rock, thanks! I always have to google this — no more!

  15. Crazy says:

    Thanks, I also keep forgetting this :) , looked it up and ended up here

Trackbacks

  1. [...] As you see, I'm using a short if/else statement (look here: http://www.lizjamieson.co.uk/2007/08…tement-in-php/) That code is equal to: PHP Code: [...]

  2. [...] As you see, I'm using a short if/else statement (look here: http://www.lizjamieson.co.uk/2007/08…tement-in-php/) I mean, if it was a whole user cp or something like that I'd go rule 5 on him too, but this is [...]

Speak Your Mind

*