Ternary Operator Inside PHP String

2020-08-12 09:21发布

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条回答
戒情不戒烟
2楼-- · 2020-08-12 09:49

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

echo ".... class='" . (1 ? "abc" : "def") . "'>....";
查看更多
登录 后发表回答