Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - if (condition) execution

let's say I have something like this:

if(1 == 0 && do_stuff()) { 
   ...
}

Obviously 1 is not 0, so there's no point to check the other condition. So does PHP ever run do_stuff() ?

like image 674
Alex Avatar asked Dec 03 '10 10:12

Alex


People also ask

How if condition works in PHP?

PHP Conditional Statements if statement - executes some code if one condition is true. if...else statement - executes some code if a condition is true and another code if that condition is false. if... elseif...else statement - executes different codes for more than two conditions.

How do you end an if statement in PHP?

<? php if (argument) { // end if statement } else if (different argument) { // end if statement } else if (another different argument) { // end if statement } else { // do something } ?>

Does else if execute after if?

The if/else statement With the if statement, a program will execute the true code block or do nothing. With the if/else statement, the program will execute either the true code block or the false code block so something is always executed with an if/else statement.

Can you put an if statement inside an if statement PHP?

We can use if statements inside if statements. These statements are called Nested If Statements.


1 Answers

No - PHP uses lazy evaluation (sometimes called short-circuit evaluation), so if the first condition in a logical AND is false, it won't attempt to evaluate any of the other conditions.

Likewise, if you were doing an OR and the first condition was true it wouldn't evaluate the second.

like image 168
John Parker Avatar answered Oct 01 '22 20:10

John Parker