Web Design Blog - May 15, 2009

PHP Ternary Operator

The ternary operator in php is a piece of code that provides some nice syntax for conditional logic. It is very useful for shorthanding an if/else block for setting a default value of a variable. For example: $v = ( empty($_GET['v']) ? '1' : $_GET['v'] ); This little snippet sets variable $v equal to the value passed in on the query for 'v', but if that value passed in on the query is empty, it sets variable $v to 1; Essentially it works like a regular conditional statement in that ( expression?... read more!

Posted in php, technology | Post Comment


View All Posts