经常在一些代码中看到defined or
的组合,例如:
defined('MyPath') or define('MyPath' , __DIR__.'/');
defined('MyPath') || define('MyPath' , __DIR__.'/');
分析一下,左边的defined
用于判断一个常量是否定义,返回boolean;
右边的define
定义常量,返回是否成功。
or或者||是短路的或,如果左边能够决定最终结果,就不会再继续执行右边的判断,即左边如果为true,右边不会被执行。
因此上述代码相当于:
if ( !defined('MyPath')){
define('MyPath',__DIR__.'/');
}