Is there any way to break?

6

I wanted to know if there is any kind of 'break' in IF . My question is based on the example below. When the b() function returns false all subsequent comparisons are not performed. I thought the condition would compare all the values and return the result.

I may be talking bullshit, but it seems that it throws a break when it finds a result that does not exceed the condition, something like throw .

$a = function(){ echo 'a'; return 1; };
$b = function(){ echo 'b'; return 1; };
$c = function(){ echo 'c'; return 1; };

if( $a() && $b() && $c() )
echo 'sucesso';

output : a b c sucesso

$a = function(){ echo 'a'; return 1; };
$b = function(){ echo 'b'; return 0; };
$c = function(){ echo 'c'; return 1; };

if( $a() && $b() && $c() )
echo 'sucesso';

output : a b

My case was to use a if where the third condition depended on the other two as true, saving if + if . In the above example it worked as expected, but it really was news to me.

    
asked by anonymous 21.09.2016 / 16:00

2 answers

9

There is something called short-circuit . Relational operators operate this way. They evaluate until they are sure of the result. When the other operands can not change the result no matter their values, it does not try to evaluate anything more and makes the decision.

This is useful because it gives better performance and avoids possible side effects that should eventually be avoided even depending on the previous condition. Then:

if ( $a() && $b() && $c() )

It's the same as:

if ( $a() )
    if ( $b() )
        if ( $c() )
            fazAlgo;

In other words, if you do not enter the first if you will not get into the others. This makes it easier to see. And interestingly, a lot of programmers write the second example without realizing they could write on a single line.

As AND requires that all operating to be true to result in true when one gives false is impossible for others to reverse the situation, it does not have to evaluate others.

The same can occur with OR . But in this case the situation is different. This operator requires that only one operand be true for the end result to be true. If the first one is true, it will no longer look at the others:

if( $a() || $b() || $c() )

It's the same as:

if ( $a() ) fazAlgo;
else if ( $b() ) fazAlgo; //faz algo igual
else if ( $c() ) fazAlgo; //tem que ser igual

Just $a() give true and $b() and $c() will not be executed. If it gives% of% it is clear that it will evaluate false , then the decision to evaluate $b() depends on the result of it.

This can be observed with other relational operators (at least in some languages).

It has language that does not adopt this.

Bit operators (eg, $c() , & ) do not have the circuit-break feature and all run at all times. They are even used when the feature is not desired.

There is a question with a classic example of its use . You have a first operand that checks whether a condition is satisfied. The second operand will only work correctly if the first operand is true. Typical verification of an object is null before accessing it.

    
21.09.2016 / 16:06
0

Yes, the argument passed in the break you get out of a level. If you have a foreach and an if:

foreach ( $var as $key )
{
  if ( $key == 0 )
  {
    #code...
    break(2);
  }
}

This way you interrupt tb the foreach. From the way you passed in the example you could use the switch as an alternative

switch (variable) {
    case 'value':
        # code...
        break;

    default:
        # code...
        break;
}
    
21.09.2016 / 16:09