I can't understand why this code is executed is not the way I want.
define('TEST', 123);
echo TEST;
echo "\n";
var_dump( defined(TEST) );
print:
123
bool(false)
Checking if a PHP Constant is Defined This can be achieved using the PHP defined() function. The defined() function takes the name of the constant to be checked as an argument and returns a value of true or false to indicate whether that constant exists.
The basic difference between these two is that const defines constants at compile time, whereas define() defines them at run time. We can't use the const keyword to declare constant in conditional blocks, while with define() we can achieve that.
Definition and Usage The define() function defines a constant. Constants are much like variables, except for the following differences: A constant's value cannot be changed after it is set.
Because you're not referring to the constant named TEST
- you're referring to whatever TEST
contains.
Wrapped out, this is what you're doing (the code is right - there is no 123
constant):
define('TEST', 123);
var_dump( defined(TEST) ); // turns into the below statement
var_dump( defined(123) ); // false - no 123 constant
Refer to the constant name instead (enclose it in quotes):
define('TEST', 123);
var_dump( defined('TEST') ); // true, the TEST constant is indeed defined
// ^ ^ Quotation marks are important!
use have call it wrong
define('TEST', 123);
echo TEST;
echo "\n";
var_dump( defined(TEST) );//provide The constant name you are providing 123 so it not defined
//correct call would be
var_dump( defined('TEST') );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With