2

where:

$b = true;
$c = 0;
$a = ($a ? ($a ? $b : $c) : ($c ? $a : $b));

I'm not sure how to work out a.

So I understand that this is a shorthand operator, and usually it's a case of:

$value ? true : false

meaning

if $a = true { true } else { false };

so:

if $a{
    if $a{
        true;}
    else{
        0;};
else{
 if $0{
    $a;}
else{
    true;}
};

does this make the value of $a true?

2 Answers2

3

The value of $a would be true

$b = true;
$c = 0;
$a = ($a ? ($a ? $b : $c) : ($c ? $a : $b));

The shorthand can be interpreted like this:

if($a) {
  if($a) {
     $a = $b;
  } else {
     $a = $c;
  }
} else {
  if($c) {
     $a = $a;
  } else {
     $a = $b;
  }
}

Because $a is false for not existing in the first place, it immediately jumps to the else statement in that. So the only part that matters to you is:

  if($c) {
     $a = $a;
  } else {
     $a = $b;
  }

0 is the same as false, so $c will come back as false, therefore $a is equal to $b, which is true.

Edit:

There is some discussion on the notice that is thrown, but this fails to account for the fact that notices are not truly errors and because of this there is no interruption to the code. The result is not Notice: Undefined variable: a, the "result" (think these people mean output) would be blank if it weren't for us determining the value of $a at the end with var_dump. The question was as to what the value of $a becomes, not what appears on your screen.

Something displaying on your screen in re to a variable not being set has nothing to do with the value of what $a is.

If you execute the following code, the notice is not the only thing realized:

$b = true;
$c = 0;
$a = ($a ? ($a ? $b : $c) : ($c ? $a : $b));

var_dump($a);

So the output is:

E_NOTICE : type 8 -- Undefined variable: a -- at line 5
bool(true)

The fact that a notice was thrown does not prevent $a from becoming true.

Also notices are easily suppressed...

error_reporting(0);
$b = true;
$c = 0;
$a = ($a ? ($a ? $b : $c) : ($c ? $a : $b));

var_dump($a);

would result in $a still becoming true, and without seeing the notice.

bool(true)
skrilled
  • 5,350
  • 2
  • 26
  • 48
  • 1
    Thank you so much, this is what I was trying to get at, this was my logic. Thank heavens!! – Jonathan Yaniv Ben Avraham Feb 24 '16 at 01:17
  • I beg to differ with this answer. If you run the code as is, you would get "Notice: Undefined variable: a in myfile.php on line 4". Take a look at my answer to see an alternative way of looking at it.. – mseifert Feb 24 '16 at 01:21
  • Notice errors are not true errors (can easily be suppressed with turning notices off), while the notice is thrown `$a` is still false at the first if statement; so if you run the code then `var_dump($a);` you will get the result `bool(true)` – skrilled Feb 24 '16 at 01:34
1

If you run the code as is, you would get: Notice: Undefined variable: a in myfile.php on line 4

Therefore, I would postulate $a is set somewhere earlier. Yet, whatever value $a has prior, if $a is can be evaluated to true or false, $a would still be true after running your code for the following reason:

If $a were true, then the first part would yield $a = $b and we know $b = true.

if(TRUE) {
  if(TRUE) {
     $a = $b;  //AND $b == TRUE
  } else {
     $a = $c;
  }
} else {
    ...
}

If $a were false, then the second part would yield $a = $b again

if(FALSE) {
    ...
} else {
  if(0) {    // 0 will equate to FALSE
     ...
  } else {
    // 0 is the same as FALSE so we end up again with $a = $b
     $a = $b;  //AND $b == TRUE
  }
}

In fact, if you run this code, it will show you the value of $a is true both times:

<?php
$a = false;
$b = true;
$c = 0;
$a = ($a ? ($a ? $b : $c) : ($c ? $a : $b));

echo $a;

$a = true;
$b = true;
$c = 0;
$a = ($a ? ($a ? $b : $c) : ($c ? $a : $b));

echo $a;
mseifert
  • 5,390
  • 9
  • 38
  • 100