Ternary Operator Inside PHP String

2020-08-12 10:00发布

问题:

I want to evaluate a simple ternary operator inside of a string and can't seem to find the correct syntax.

My code looks like this:

foreach ($this->team_bumpbox as $index=>$member) 
    echo ".... class='{((1) ? abc : def)}'>....";

but I can't seem to get it to work properly. Any ideas on how to implement this?

回答1:

You can't do it inside the string, per se. You need to dot-concatenate. Something like this:

echo ".... class='" . (1 ? "abc" : "def") . "'>....";