【发布时间】:2021-10-31 16:51:08
【问题描述】:
我正在尝试在 PHP 中的字符串中使用三元运算符
"hard_copy"=>"<input type='checkbox' (($station->hard_copy==1)?'checked':'') name='station[]' id='hard-copy-$key' class='hard-copy' value='hardcopy-$station->id-1' >
<label for='hard-copy-$key' style='margin-left: 30%;'></label>",
但我可能有一些语法错误
【问题讨论】:
-
你能把错误包括进来吗
-
没有错误我试图根据条件检查复选框并且复选框未被选中
-
您不能像这样在双引号字符串中解压缩语句,只能解压缩变量。您需要使用串联。
"foo " . ($a == 1 ? 'checked' : '') . " bar" -
如果您遇到错误,请始终在您的问题中包含该错误。
-
你不需要展示你的整个班级。您的问题已经得到解答,无论是在 cmets 中还是作为真正的答案。
标签: php