Over a million developers have joined DZone.

PHP Shorthand Syntax

·
Handy shorthand syntax for php. From: http://www.tech-recipes.com/php_programming_tips288.html


A compact (if cryptic) shorthand is useful: 


echo "var is ".($var < 0 ? "negative" : "positive"); 


is equivalent to: 


echo "var is "; 
if ($var < 0) 
    echo "negative"; 
else 
    echo "positive"; 


The shorthand can be considered (expression ? true_value : false_value) where 'expression' is evaluated and, if true, the 'true_value' is returned, otherwise the 'false_value' is returned.



Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}