I would like to use constants in PHP but "WEB"
gets interpreted as false
. I never used constants before, what do I miss?
define("WEB", true);
define("MOBILE", false);
define("DESKTOP", false);
if (defined('MOBILE' == true) || defined('DESKTOP' == true) ){
echo "MOBILE or DESKTOP";
} else if (defined('WEB' == true)) {
echo "WEB";
}
You are using defined()
incorrectly. You are not checking if the constants are defined. You are checking their values. Just check them like you would a variable:
if (MOBILE == true || DESKTOP == true ){
echo "MOBILE or DESKTOP";
} else if (WEB == true) {
echo "WEB";
}
Which can be shortened to:
if (MOBILE || DESKTOP){
echo "MOBILE or DESKTOP";
} else if (WEB) {
echo "WEB";
}
defined
gives you whether or not the constant exists, not its value.
if (MOBILE || DESKTOP){
echo "MOBILE or DESKTOP";
} else if (WEB) {
echo "WEB";
}
If you want to check if constant is defined, your code should be:
define("WEB", true);
define("MOBILE", false);
define("DESKTOP", false);
if(defined('MOBILE') || defined('DESKTOP'))
echo "either MOBILE or DESKTOP is defined";
elseif(defined('WEB')) echo "WEB is defined";
If you want to check if constant value is true or false it would be
if(MOBILE || DESKTOP)
echo "either MOBILE or DESKTOP is true";
elseif(WEB) echo "WEB is true";
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.