我不知道为什么我会收到解析错误:语法错误,第 6 行出现意外T_IF。
$sf = array (
"name" => $firstname . ' ' . $lastname,
"email" => $email,
"address" => $shipping_address_1 . if(!empty($shipping_address_2 )) { echo ", " . $shipping_address_2 }
);
print_r($sf);
我想检查 $shipping_address_2 是否不为空,如果不是,则显示它。
因为你的代码是错误的。不能在数组初始化中放置 if
语句。
不是 100% 确定,但您应该能够使用三元运算符...
"address" => $shipping_address_1 .
(!empty($shipping_address_2 )) ?
", " . $shipping_address_2 : // This gets executed if condition is true
"" // This gets executed if the condition is false
替换
"address" => $shipping_address_1 . if(!empty($shipping_address_2 )) { echo ", " . $shipping_address_2 }
跟
"address" => $shipping_address_1 . (empty($shipping_address_2) ? null : ", " . $shipping_address_2)