Reputation: 4003
I have the following ternary operator:
<?php (strcmp($activetab,'profile') == 0 ? echo "active" : ''); ?>
Unfortunately it is not working as expected. I wish that if the condition was true that active
would be echoed, otherwise nothing.
What am I doing wrong?
Upvotes: 0
Views: 125
Reputation: 11832
The $comp ? $a : $b
syntax is not really shorthand for just
if ( $comp ) { $a; } else { $b }
It is actually kind of:
function ($comp, $a, $b) { if ( $comp ) { return $a; } else { return $b; } }
So indeed try echo (strcmp($activetab,'profile') == 0 ? "active" : '');
Upvotes: 3
Reputation: 12815
Simply change your code like this:
<?php echo (strcmp($activetab,'profile') == 0 ? "active" : ''); ?>
Upvotes: 1
Reputation: 2129
Try now
<?php echo (strcmp($activetab,'profile') == 0) ? "active" : ''; ?>
Upvotes: 5