处理PHP中的大型IF语句 [英] Dealing with big IF statements in PHP
本文介绍了处理PHP中的大型IF语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
PHP中的简单 if
语句有什么好的选择吗?我知道切换
,但我猜这里还有一些更精致的替代方案,当使用非常大的时会很方便
陈述。
Is there any good alternative for the plain if
statements in PHP? I know about switch
, but I'll guess that there's some more refined alternative out there that comes handy when working with really big if
statements.
非常感谢,
推荐答案
如果你想只提高可读性,那么你总是可以在if语句中拆分表达式:
If you want to improve readability only, then you can always split up the expressions inside the if statement:
$exp1 = is_array($var) && isset($var['key']);
$exp2 = is_object($var) && isset($var->key);
$exp3 = substr($string, 0, 4) == 'foo';
$exp4 = ($exp1 || $exp2) && $exp3;
if ($exp4) {}
而不是
if (((is_array($var) && isset($var['key'])) || (is_object($var) && isset($var->key))) && substr($string, 0, 4) == 'foo') {}
显然,这些是简化的例子,但你明白了......
Obviously, these are simplified examples, but you get the idea...
这篇关于处理PHP中的大型IF语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文