比如
$order['status'] == 9 || $order['status'] == 10 || $order['status'] == 11
类似这种的话,如果两三个还勉强可以接受,但是如果还有 3 个以上的条件我就无法接受这种写法了。
后来使用了 in_array()的方法相对简短一点了。
想来请教下 php 中 有没有比 in_array()再好点的写法了。谢谢
比如
$order['status'] == 9 || $order['status'] == 10 || $order['status'] == 11
类似这种的话,如果两三个还勉强可以接受,但是如果还有 3 个以上的条件我就无法接受这种写法了。
后来使用了 in_array()的方法相对简短一点了。
想来请教下 php 中 有没有比 in_array()再好点的写法了。谢谢
像你这个例子, 改成switch就行了,简单直观还合理;
如果条件太多无可避免的情况,即只能用if,就算用别的也感觉别扭不对劲的时候, 建议换一种写法:
if ($order['status'] == 9
|| $order['status'] == 10
|| $order['status'] == 11) {
}
我是花括号部另起一行党, 但在这种情况下,也可以
if ($order['status'] == 9
|| $order['status'] == 10
|| $order['status'] == 11)
{
}
另外建议把字面量常量放在左边。
4 回答14k 阅读✓ 已解决
5 回答8.3k 阅读✓ 已解决
1 回答3.7k 阅读✓ 已解决
3 回答1.6k 阅读✓ 已解决
2 回答2k 阅读✓ 已解决
2 回答2k 阅读
1 回答1.1k 阅读✓ 已解决
酱?