If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
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 and 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.
$x = ($myvalue == 10) ? "the value is 10": "the value is not 10";
In the line of code above, if the value of the variable $x is equal to 10, the string “the value is 10″ is printed out. If the value of $x is anything other than 10, the string “the value is not 10″ is printed out.
















{ 6 comments… read them below or add one }
Thanks, I also keep forgetting this :), looked it up and ended up here
You rock, thanks! I always have to google this — no more!
I’ve always wanted someone to tell me that. Thank you!
Nice one, just what i’m looking for!
Useful and quick find, thanks much.
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.