[英]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?我可以检查常量是否已定义,但是如何检查常量的值以在 if 语句中使用它?
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. defined()
函数检查给定的常量是否存在。
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';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.