Reputation: 2693
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";
}
Upvotes: 0
Views: 103
Reputation: 1356
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";
Upvotes: 0
Reputation: 180177
defined
gives you whether or not the constant exists, not its value.
if (MOBILE || DESKTOP){
echo "MOBILE or DESKTOP";
} else if (WEB) {
echo "WEB";
}
Upvotes: 0
Reputation: 219924
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";
}
Upvotes: 4