sys_debug
sys_debug

Reputation: 4003

Incorrect shorthand if else statement

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

Answers (3)

nl-x
nl-x

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

Viktor S.
Viktor S.

Reputation: 12815

Simply change your code like this:

<?php echo (strcmp($activetab,'profile') == 0 ?  "active" : '');  ?>

Upvotes: 1

Praveen kalal
Praveen kalal

Reputation: 2129

Try now

<?php echo (strcmp($activetab,'profile') == 0) ?  "active" : '';  ?>

Upvotes: 5

Related Questions