2

Probably really easy for a pro, but could someone re-write this from it's PHP shorthand form to non-shorthand?

($facebook) ? $fb_active_session = $facebook->fbc_is_session_active() : $fb_active_session = false;

Thanks!

1
  • 1
    Ternary operator - (condition) ? true : false ;
    – Sampson
    Jul 1, 2009 at 12:00

4 Answers 4

7
if($facebook) {
  $fb_active_session = $facebook->fbc_is_session_active(); 
} else {
  $fb_active_session = false;
}
2
  • Here's proof that not everyone finds the ternary operator easy to read.
    – karim79
    Jul 1, 2009 at 12:06
  • I thought 4 characters was the standard.
    – Sam152
    Jul 1, 2009 at 12:13
6

It would have been better and more clearly written (still using the ternary op) as:

$fb_active_session = ($facebook) ? $facebook->fbc_is_session_active() : false;
4
$fb_active_session = false;
if($facebook)
{
    $fb_active_session = $facebook->fbc_is_session_active();
}
1

Think of it like this:

IS $facebook TRUE ? THEN DO THIS : IF NOT, THIS ;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.