简体   繁体   中英

How to check if the value of a PHP defined constant equals something?

using this:

defined('WPLANG')

I'm able to check whether the constant is defined or not, but how can I check the value of the constant to use it in an if statement?

define('WPLANG', 'some value');
if(WPLANG == 'some value'){
  ... 
  ...
}

Or

define('WPLANG', 1212);
if(WPLANG == 1212){
   ... 
   ...
}
<?php

define('WPLANG','hello');

if(WPLANG == 'hello') {
  echo 'YES';
}

?>

Below is my preferred way.

// Testing if WPLANG exists
if ( !defined('WPLANG') ){
    define('WPLANG', 'wplang_value');
} 

// Testing if WPLANG equals a certain value
if ( WPLANG == 'wplang_value' ){
    echo WPLANG; 
}

The defined() function checks whether or not a given constant exists.

if(defined('WPLANG'))
    echo 'exists';

You can compare constants in the same way as variable, ie <, >, ==, etc. So if you wanted to check the value of a constant, simply do this:

if(WPLANG == 'en-US')
    echo 'set to en-US';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM