Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If first argument falls true in a condition with and operators will the next run?

Lets say I've got a pice of code that looks like this:

if( !isset($this->domainID) && !$this->getDomainID() ){
    return false;
}

Will the 2nd statement run if the first one is true? Because performance wise it would be stupid to get the ID from the database if I've already got it and there's a lot of other situation where the same apply. If it doesn't I'd have to nest them, am I right?

I don't know if there's an standard on how programming language work in these cases or if it's different in other languages then php. I tried googling it but I didn't really know what to search for in this case. As you can see I hade a quite hard time describing it in the title.

like image 269
Hultner Avatar asked Apr 28 '11 11:04

Hultner


People also ask

What will happen if the first condition fails in a Boolean expression with and operator?

No, if the first condition returns false then the whole expression automatically returns false. Java will not bother examining the other condition.

Which operator is used if the statement evaluates true only if both the expressions are true?

The logical OR operator ( || ) returns the boolean value true if either or both operands is true and returns false otherwise.

Which of the operators do we use if we want at least one condition to be true?

Logical operators: AND, OR and NOT|| — OR; allows you to chain together two or more expressions so that one or more of them have to individually evaluate to true for the whole expression to return true .

Why logical operators are used in conditional statements?

We sometimes want to combine multiple comparisons together in a conditional expression, and that's why we have logical operators. These operators let us say things like "if both X and Y are true" or "if either X or Y are true" in our programs.


3 Answers

Yes. If the first is true, the second will be evaluated. Conversely, if the first is false, the second will not be evaluated. This can be a good place for micro optimizations and, as you noted, logical progression.

Per the comments, the inverse is true for OR conditions. So if the first expression is false the next will be evaluated and if the first expression is true the next will not.

like image 58
Jason McCreary Avatar answered Oct 05 '22 23:10

Jason McCreary


Answer is yes, you can see it by yourself by doing something like this:

#!/usr/bin/php                                                                  
<?PHP
function test1() {
    echo "inside test1\n";
    return false;
}
function test2() {
    echo "inside test2\n";
    return true;
}
echo "test1 first AND test2 second\n";
if (test1() && test2()) {
    echo "both passed\n";
}
echo "test2 first AND test1 second\n";
if (test2() && test1()) {
    echo "both passed\n";
}
echo "test1 first OR test2 second\n";
if (test1() || test2()) {
    echo "one passed\n";
}
echo "test2 first OR test1 second\n";
if (test2() || test1()) {
    echo "one passed\n";
}
?>
like image 32
rasjani Avatar answered Oct 06 '22 00:10

rasjani


No, if the first is false, the second will not be evaluated.

What you're really talking about here is "lazy evaluation". It's often used in functional programming languages and can vastly improve the run-time. See here for more info: http://en.wikipedia.org/wiki/Lazy_evaluation

PHP does not use lazy evaluation but in conditionals like this it does at least stop before the second argument if the result is already clear.

like image 41
Nick Brunt Avatar answered Oct 05 '22 23:10

Nick Brunt