Reputation: 227
Basically I have the following code:
<a href="<?php echo (!$isLoggedIn && !isset($_COOKIE['access_token'])) ? "connect.php" : isset($_GET['autoooooo']) ? "main.php?auto=true" :""?>"></a>
However when I var_dump
$isLoggedIn
and $isset($_COOKIE['access_token'])
both are false (so through the ! in the <?php?>
it gets true) and the href
should be connect.php
... but it always is main.php?auto=true"
.autoooooo
does not even exist (I just made it for testing) and href
should actually be empty.
What am I doing wrong?
Upvotes: 0
Views: 56
Reputation: 17720
In php, the ternary operator (?:
) associates left-to-right (unlike in C or perl where it associates right-to-left).
That means that it evaluates the first test ? value 1 : value 2
, and then uses that result to determine which value of the second operator to use.
Your construct would work in C or perl, but in php, you need to add brackets around each subsequent ternary operator.
Also, for readability, I recommend you add quite a few newlines and indents in your code.
Upvotes: 1
Reputation: 257
Use brackets for your 2nd clause:
$b1 = false;
$b2 = false;
echo $b1 ? 'b1 true' : ($b2 ? 'b2 true' : 'all false') ;
Output:
all false
Upvotes: 0