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,930
  • 31
  • 110
  • 182
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,833
  • 7
  • 38
  • 58
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,930
  • 31
  • 110
  • 182
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>";
GautamD31
  • 28,552
  • 10
  • 64
  • 85
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,230
  • 2
  • 29
  • 61
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
  • 627
  • 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,424
  • 18
  • 72
  • 100