0

I am trying to use an if condition within a variable containing html. I am getting errors with a standard condition and a ternary condition. Am I going about this the right way, or is there a different way I should be doing this?

Both methods I have tried:

$test = "
 <tr>
   <th><a href=\"installs.php?column=username&order=" . $asc_or_desc . "\">Username<i class=\"fas fa-sort" . if($column == 'username'){ '-' . $up_or_down; } else {''} . "\"></i></a></th>
 </tr>
";

and tarnery:

$test = "
 <tr>
  <th><a href=\"installs.php?column=username&order=" . $asc_or_desc . "\">Username<i class=\"fas fa-sort" . $column == 'username' ? '-' . $up_or_down : '' . "\"></i></a></th>
 </tr>
"; 
3
  • What do both of the above approaches return right now and what output did you want instead? Commented Jun 8, 2020 at 22:48
  • 1
    if cannot be concatenated in PHP. The ternary operator's result can, but you probably should use brackets, as if to not have the concatenation precede the ternary operator. Commented Jun 8, 2020 at 22:54
  • What's the error? Commented Jun 8, 2020 at 23:13

1 Answer 1

2

You need to use brackets around your operator, try this:

$test = "
 <tr>
  <th><a href=\"installs.php?column=username&order=" . $asc_or_desc . "\">Username<i class=\"fas fa-sort" . ($column == 'username' ? '-' . $up_or_down : '') . "\"></i></a></th>
 </tr>
"; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.