I'm stepping through the source code of CodeIgniter with Xdebug in NetBeans and I'm looking for a way to see defined constants as they are defined. If it's not possible, are there any other ways to display all defined constants?
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.
A constant is an identifier (name) for a simple value. The value cannot be changed during the script. A valid constant name starts with a letter or underscore (no $ sign before the constant name). Note: Unlike variables, constants are automatically global across the entire script.
Constants can be defined using the const keyword, or by using the define()-function. While define() allows a constant to be defined to an arbitrary expression, the const keyword has restrictions as outlined in the next paragraph. Once a constant is defined, it can never be changed or undefined.
A constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. If you have defined a constant, it can never be changed or undefined. To define a constant you have to use define() function and to retrieve the value of a constant, you have to simply specifying its name.
Take a look at the get_defined_constants
function. It will return an array of all the defined constants in the code up to the point of the function call. You can then use print_r
to print out the array.
This kind of practice I use is quite decent as it shows only custom/user created constants.
print_r(var_export(get_defined_constants(true)['user'], true));
Wrap this within pre tags or just view source and you'll get very nice array of all the stuff You defined.
Note that this is not going to work with php 5.3.* where in 5.4.* it outputs fine.
In earlier versions of php, get_defined_constants()
must be assigned to a variable first, before output.
Something like this.
$gdc = get_defined_constants(true);
print_r( var_export($gdc['user'], true) );
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