我写了一个循环,它打印具有多个 OR 条件的特定文本,但是在最后一项中,我不想打印 OR 条件。
for ($x = 1; $x <= $vc; $x++) {
echo "!empty("."$"."${"a".$x}) || ";
}
当前输出是
if(!empty($user_usertype) || !empty($user_drinking) || !empty($user_smoking) || )
我希望输出为
if(!empty($user_usertype) || !empty($user_drinking) || !empty($user_smoking))
即 OR 条件不应在最后一次迭代中出现。
使用三元运算符或 if 语句:
for ($x = 1; $x <= $vc; $x++) {
echo "!empty("."$"."${"a".$x})" . ($x == $vc ? "" : "|| ");
}
是相同的:
for ($x = 1; $x <= $vc; $x++) {
echo "!empty("."$"."${"a".$x})";
if($x != $vc)
echo "|| ";
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句