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";

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

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

Comment Form

Featured Sponsors


  • Curt: If anyone comes across this with similar issues I was able to sort out the pagination issues painlessly with easyCommentsPaginate from http://www.mush
  • Christopher: Yeah, it is indeed hard to do. And something remains elusive about why the pagination never worked. I tried everything I could find. Regardless, I
  • Jennifer: Hi Christopher, always hard to bug test stuff like that remotely. Sorry those didn't help. Glad you found a solution though :)

About


Advertisements