-2

I have seen in many situations on Stack overflow, I see people using examples containing:

$Var = isset($_SESSION['Key'])? $_SESSION['Key']: null;

What is the actual structure and representation? is there any performance improvements server sided with using these operators compared to a simple if and else statement? or is this generally down to personal programming preference?

user2146021
  • 47
  • 3
  • 9
  • 1
    http://php.net/manual/en/language.operators.comparison.php – Brian Roach Mar 21 '13 at 02:23
  • I'd be suprised if there was any performance difference. It's perfect for setting default values to variables, but it remains a matter of preference. – Tchoupi Mar 21 '13 at 02:24
  • Personal preference, concise code, clarity of variable assignments. It's called the _ternary operator_ – Michael Berkowski Mar 21 '13 at 02:24
  • Yes, the manual is clear; but it does not specify performance hits, best practices etc. – user2146021 Mar 21 '13 at 02:25
  • So it's mainly preference? – user2146021 Mar 21 '13 at 02:26
  • keep in mind, this is a high level langague, and its likely that the actual code executed via if-else vs ? is identical after compilation. I personally like being able to present that kind of operation as one line, as it makes clear that one thing is being done. – Frank Thomas Mar 21 '13 at 02:26
  • I'm not sure about PHP specifically, but couldn't this become a conditional move instead of a jump possibly after compilation? – asimes Mar 21 '13 at 02:30

1 Answers1

1

It is just to produce shorter code. It is equivalent to:

if (someCondition) // do if true
else // do otherwise

or for your code...

if ($_SESSION['Key']) $Var = $_SESSION['Key'];
else $Var = null;
asimes
  • 5,749
  • 5
  • 39
  • 76