scriptygoddess

08 Feb, 2007

PHP if statement in shorthand

Posted by: Jennifer In: Bookmarks

Another "tired of hunting for the exact syntax" things… Here's shorthand for an "if" statement:

echo isset($var) ? $var : "sorry, nothing here";

2 Responses to "PHP if statement in shorthand"

1 | Sunny

February 8th, 2007 at 12:22 pm

Avatar

Also called a ternary operator. The outer parentheses can be dropped, too.

2 | Web Developer, James Radford

August 10th, 2009 at 3:50 am

Avatar

yup,

if(isset($var))
echo $var;
else
echo "sorry, nothing here";

can lead to problems if developers write immediately after the 'sorry, nothing here' message expecting it to be outside the conditional statement but otherwise this example can keep readability high whilst reducing the number of lines of code.

cheers,
James

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements