3

I'm still a newbie in php and I'm using codeigniter for my backend framework. I have a table.php file that will generate a html table in real-time. Then, I encounter some issues.

$output_string .= "<td>".($row->isactive == "0") ? "Activated":"Deactivated"."</td>";

with the above code I get nothing, but with a little change to:

$isactive = ($row->isactive == "0") ? "Activated":"Deactivated";
$output_string .= "<td>".$isactive."</td>";

I get my results, so my question is, why?

Doesn't PHP support question mark operator in string concatenation??

Lucas
  • 16,410
  • 29
  • 105
  • 173
Jerry Lam
  • 452
  • 1
  • 7
  • 21
  • 2
    In addition to the answers listed below, it should be noted that the `?` operator in `PHP` acts a little differently than the same operator in **every other language.** The problem is that PHP, unlike all other languages, makes the conditional operator left associative. This breaks your code – which would be fine in other languages. -> from http://stackoverflow.com/questions/6203026/how-to-concatenate-multiple-ternary-operator-in-php – Jeremy J Starcher Sep 27 '12 at 05:03

6 Answers6

2

It does support it, just put some parenthesis around it:

$output_string .= "<td>".(($row->isactive == "0") ? "Activated":"Deactivated")."</td>";
Matt Dodge
  • 10,374
  • 6
  • 35
  • 56
1

You're not putting enough parentheses. Try this instead:

$output_string .= "<td>".(($row->isactive == "0") ? "Activated":"Deactivated")."</td>";

Note the extra set of parentheses.

If you just put:

$output_string .= "<td>".($row->isactive == "0") ? "Activated":"Deactivated"."</td>";

The PHP interpreter will try and display $row->isactive == "0", so for it to do what you want, you must enclose it in an extra set of parentheses.

Lucas
  • 16,410
  • 29
  • 105
  • 173
1

Not like that,but after the ":" operator your

:"Deactivated"."</td>";

should be treated as single statement for false,if you want to got this try like

$output_string .= "<td>".(($row->isactive == "0") ? "Activated":"Deactivated")."</td>";
Gautam3164
  • 28,027
  • 10
  • 58
  • 83
1

You should wrap the entire ternary operation in a () brackets

$output_string .= "<td>".($row->isactive == "0" ? "Activated":"Deactivated")."</td>";

That will give you what you want.

My guess of what was happening is, "<td>" is appened the boolean result of ($row->isactive == "0") and you would always get "Activated</td>" as your result.

Prasanth
  • 5,109
  • 2
  • 27
  • 59
1

Concatenation in PHP have higher priority than ternary operator. So first execute expression "<td>".($row->isactive == "0"), then result of it expression (it allways is equal true, because convertation non-empty string to boolean value interpreted as true). So, result of your code always is word Activated.

doktorgradus
  • 637
  • 1
  • 5
  • 13
0

It will work when you use () after and before concatenation like this

$output_string .= "<td>".(($row->isactive == "0") ? "Activated":"Deactivated")."</td>";
Yogesh Suthar
  • 30,136
  • 18
  • 69
  • 98