Alex
Alex

Reputation: 68440

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() ?

Upvotes: 23

Views: 4496

Answers (3)

Adeel
Adeel

Reputation: 615

NO, it'll not execute do_stuff() in this condition.

Upvotes: 1

Pradeep Singh
Pradeep Singh

Reputation: 3634

If first condition is false then php never run the second condition in && operator

Upvotes: 4

John Parker
John Parker

Reputation: 54445

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.

Upvotes: 49

Related Questions