Php 逆条件
我试图重新表述a条件以删除Php 逆条件,php,conditional,Php,Conditional,我试图重新表述a条件以删除{..}语句(使代码不那么缩进) 目前我有: while($something){ if((strcasecmp($str1, $str2) === 0) || (isset($arr[0]) && strcasecmp($str3, $str4) === 0)){ // a lot of code here... break; } } 使用反转IF条件时,其应如下所示: while($something){
{..}
语句(使代码不那么缩进)
目前我有:
while($something){
if((strcasecmp($str1, $str2) === 0)
|| (isset($arr[0]) && strcasecmp($str3, $str4) === 0)){
// a lot of code here...
break;
}
}
使用反转IF条件时,其应如下所示:
while($something){
if((strcasecmp($str1, $str2) !== 0)
&& (empty($arr[0]) && strcasecmp($str3, $str4) !== 0))
continue;
// a lot of code here...
break;
}
但它不起作用。我的代码和break语句在不应该执行的时候被执行
我做错了什么。这应该行得通
while($something){
if((strcasecmp($str1, $str2) !== 0)
&& (!isset($arr[0]) || strcasecmp($str3, $str4) !== 0))
continue;
// a lot of code here...
break;
}
这里
&&
必须是|
我个人会保留第一个变体,因为它更直接
更新:好的,当我想到它时,break代码>让我好奇。你想摆脱一个意图吗
if ($something
&& ((strcasecmp($str1, $str2) === 0) || (isset($arr[0]) && strcasecmp($str3, $str4)) === 0)
){
// a lot of code here...
}
并了解一些微观优化:)(!stracecmp()
意味着它们是相等的,除了可能的情况)
我希望这两个论点是一致的 老实说,我不会重构您的代码来摆脱{..}
。如果导致了问题,如果增加了可读性等,则进行重构,但这两种方法都不适用于这种情况。一种简单的方法是执行!(旧条件)
中断有什么好处?就我所见,而($x){/*../break;}
与相同,如果($x){/*../}
:?@KingCrunch哈哈,这应该是答案^ ^我甚至没有注意到这个愚蠢lol@KingCrunch如果$something
是$arr=$stmt->fetch()<代码>空
与不同!isset
。事实上,empty(0)
是正确的,但是!isset(0)
isfalse@Alfwed你是对的,但是我采用了未重构的变体,因此这一个应该像预期的那样工作。
if ($something
&& ((strcasecmp($str1, $str2) === 0) || (isset($arr[0]) && strcasecmp($str3, $str4)) === 0)
){
// a lot of code here...
}
if ($something && (!strcasecmp($str1, $str2) || (isset($arr[0]) && !strcasecmp($str3, $str4))) {
// a lot of code here...
}