Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP {$condition} && execute();

I've always used this code to run one line if statements.

$variable = TRUE; // or anything that evaluates to TRUE
$variable && execute_code();

Basically it would run the function if $variable is TRUE.

My question: What is the correct term for doing it this way?

like image 599
rgin Avatar asked Jul 10 '13 08:07

rgin


People also ask

How does PHP condition work?

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 } ?>

What is nested if...else statement in PHP?

The nested if statement contains the if block inside another if block. The inner if statement executes only when specified condition in outer if statement is true.


1 Answers

It is called Short-circuit evaluation.

The short-circuit expression x Sand y (using Sand to denote the short-circuit variety) is equivalent to the conditional expression if x then y else false; the expression x Sor y is equivalent to if x then true else y.

like image 181
sectus Avatar answered Sep 18 '22 02:09

sectus