0

What does this line of PHP code mean? That is, the question mark bit followed by true, colon, false?

return(isset($_SESSION['id'])) ? true : false;
William Perron
  • 485
  • 7
  • 16
which1ispink
  • 3,018
  • 3
  • 14
  • 24
  • Shorthand for if(a=b){c}else{d} – Moe Tsao Jan 03 '14 at 23:35
  • 4
    It's actually useless in this scenario. `return isset($_SESSION['id']);` does exactly the same thing. Someone was trying to get fancy and ended up looking like an idiot. – jeremy Jan 03 '14 at 23:36

5 Answers5

1

It's a ternary operator.

It's simply short-hand for:

if (isset($_SESSION['id']))
  return true;
else
  return false;
kba
  • 19,333
  • 5
  • 62
  • 89
1

Same as:

if isset($_SESSION['id']) {
     return true;
} else {
     return false;
}
misakm
  • 122
  • 6
1

This syntax is for Ternary operators in PHP

It runs like (Condition to evalute) ?( Return result if condition is true) : (return result if condition is false)

in your case return(isset($_SESSION['id'])) ? true : false;

if $_SESSION['id'] is set it will return true and if session is not set it will return false.

? mark is equivalent for if statement while : is for else

It is short form of if else statement

link : http://davidwalsh.name/php-shorthand-if-else-ternary-operators

Maz I
  • 3,664
  • 2
  • 23
  • 38
0

This is a ternary operator.short for

if(isset($_SESSION['id'])){ 
   return true;
 }else{ return false;}

However this is useless because isset() already returns true or false

Alex Shilman
  • 1,547
  • 1
  • 11
  • 15
-1

? boolean-condition [action] : [action] is the ternary conditional operator and is short for:

if ([condition]) { [action] } else { [action] }

hd1
  • 33,938
  • 5
  • 80
  • 91