PHP {$ condition} && execute();

rgi*_*gin 5 php terminology

我总是使用这段代码运行一行if语句.

$variable = TRUE; // or anything that evaluates to TRUE
$variable && execute_code();
Run Code Online (Sandbox Code Playgroud)

基本上,如果$variable为TRUE ,它将运行该函数.

我的问题:这样做的正确用法是什么?

sec*_*tus 7

它被称为短路评估.

短路表达x Sand y(使用砂来表示短路品种)等效于条件表达式if x then y else false;的表达式x Sor y相当于if x then true else y.

  • 这整天都在扼杀我.谢谢! (2认同)