-3

I have the following example:

$a=false;
$b=true;
$c=false;
if($a ? $b : $c){
   echo 'false';
} else {
   echo 'true';
}

I can't seem to understand this statement,and i need someone to explain me how it works...

Petru Lebada
  • 2,167
  • 8
  • 38
  • 59
  • 3
    I'm voting to close this question as off-topic because it is vague, broad and probably just involves reading the manual. – PeeHaa Mar 30 '15 at 08:46
  • 1
    The only thing you need to do to understand this is "unpack" the ternary expression `$a ? $b : $c` then replace the result inside the `if` [Ternary Operator](http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary) – kalatabe Mar 30 '15 at 08:47

2 Answers2

1
$a=false;
$b=true;
$c=false;
if($a ? $b : $c){
   echo 'false';
} else {
   echo 'true';
}

expands to:

$a=false;
$b=true;
$c=false;
if ($a) {
  $temp = $b; // TRUE
} else {
  $temp = $c; //FALSE
}
if($temp){
   echo 'false';
} else {
   echo 'true';
}

because $a is false, $temp is assigned $c value (which is false), second if checks if $temp is true (which is not), so else statement is executed echo 'true'

Bogdan Kuštan
  • 5,427
  • 1
  • 21
  • 30
1

It's an equivalent of this:

<?php

$a = false;
$b = true;
$c = false;

if(($a && $b) || (!$a && $c)) {
  echo 'false';
} else {
  echo 'true';
}
tmt
  • 7,611
  • 4
  • 32
  • 46
  • 2
    Hmm, now I wonder if somebody considers my answer wrong or if somebody is getting upset that I'm answering a question he/she thinks should not be here at all. – tmt Mar 30 '15 at 09:06