如何检查 PHP 定义的常量的值是否等于某个值?

Moh*_*aid 2 php constants

使用这个:

defined('WPLANG')
Run Code Online (Sandbox Code Playgroud)

我可以检查常量是否已定义,但如何检查常量的值以在 if 语句中使用它?

Gau*_*rav 5

define('WPLANG', 'some value');
if(WPLANG == 'some value'){
  ... 
  ...
}
Run Code Online (Sandbox Code Playgroud)

或者

define('WPLANG', 1212);
if(WPLANG == 1212){
   ... 
   ...
}
Run Code Online (Sandbox Code Playgroud)

最好在检查常量的值之前检查该常量是否已定义,因为它if ( I_AM_UNDEFINED )总是false,因为未定义常量的值为null。如果你假设它存在而它不存在,那么你可能会得到一些意想不到的结果。所以:

if( defined('WPLANG') && WPLANG === 'some value' ) {
Run Code Online (Sandbox Code Playgroud)