How can I simplify this logic

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

if(($x || $a) && ($x || $y))
{
   if($x){
          return true;
   }
}
return false;
+3
source share
8 answers
if ($x) return true;
return false;

According to your statement, you return true only if it $xis true, so is this the only statement that you really need to check, correct? Variables $aand $yhave no value.

Edit: Same as:

return (bool) $x;
+6
source

If you return true only if $ x is true, then the rest of the code does not matter. In this way,

return (bool) $x;

EDIT: Oops... bool - , .

+4

if, ($x || $a) && ($x || $y) $x || ($a && $y). , $x ( if), ($x || ($a && $y)) && $x. $x && $x || $x && $a && $y, $x || $x && $a && $y. OR $x . $x , .

, true, $x:

return (bool) $x;
+2

, $x. , :

return $x
+1
   if($x){
          return true;
   }

   return false;

?

Edit:

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

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

EDIT: return $x;

0

You can write this as a single line expression ...

return $x;

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

0
source

Source: https://habr.com/ru/post/1736443/


All Articles