开发者

How can I simplfy this logic

I'm having trouble simplifying this conditional statements logic. Is there a more effervescent way of writing this?

if(($x || $a) && ($x || $y))
{
   if($x){
          return true;
   开发者_如何学Go}
}
return false;


if ($x) return true;
return false;

According to your statement, you only return true if $x is true, therefore that is the only statement you really need to check for, correct? The variables $a and $y are completely irrelevant.

Edit: Same thing as:

return (bool) $x;


If you only return true if $x is true, then the rest of the code is irrelevant. Thus,

return (bool) $x;

EDIT: Oops... bool is a cast, not a function.


The condition of the outer if, ($x || $a) && ($x || $y), is equivalent to $x || ($a && $y). When we conjunct that with the condition that $x must also be true (inner if), we get ($x || ($a && $y)) && $x. And that is equivalent to $x && $x || $x && $a && $y which can be reduced to $x || $x && $a && $y. In both OR branches $x must be true to proceed. But if the $x in the right branch is true, the whole condition is already true.

So the only variable that needs to be true is $x:

return (bool) $x;


Like several people have already said, only $x matters in your code. I guess the shortest possible code is:

return $x


if($x && ($a || $y))
     return true;

return false;


if(($x && $a && $y)
{
    return true;
}
return false;

EDIT : it is simply return $x;


You could write this as a one line expression...

return $x;

regardless of $a and $y, $x has to be true to return true


   if($x){
          return true;
   }

   return false;

?

Edit:

return $x
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜