Short If Statement in PHP

by Liz Jamieson on August 20, 2007

in php

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.

  • Digg
  • MisterWong
  • Reddit
  • SphereIt
  • StumbleUpon
  • Technorati
  • del.icio.us

Related Posts

{ 6 comments… read them below or add one }

Crazy 04.30.08 at 4:19 pm

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

Mike 05.27.08 at 12:48 pm

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

Liz Jamieson 05.30.08 at 2:00 pm

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

website bouwers apeldoorn 06.10.08 at 11:50 am

Nice one, just what i’m looking for!

Charlie S 08.08.08 at 12:18 am

Useful and quick find, thanks much.

Jake 10.16.08 at 10:23 am

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.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post: The Apple : A Writing Exercise

Next post: Pre-select Not Working in Firefox for XHTML SELECT