15

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?

4 Answers 4

14

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.

3
  • One of the nicer features of PHP is that it has almost everything you will ever need.
    – MitMaro
    Dec 8, 2009 at 1:47
  • Hence the annoyed grunt. ;-) Still waiting to see if anyone knows if Xdebug can display constants though. Dec 8, 2009 at 1:48
  • Xdebug doesn't provide this functionality. No reason you couldn't use the Xdebug var_dump to display the constants though.
    – MitMaro
    Dec 8, 2009 at 1:56
5

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) );
1
  • Nice solution to avoid displaying everything (Super Globals, etc.). Avoids the more elements... at the end of the listing
    – Sablefoste
    Sep 13, 2017 at 19:10
3

You probably want to adapt:

$arr = get_defined_vars();

1
  • Variables and Constants are two different worlds... therefore, no, he probably doesn't want to adapt get_defined_vars()
    – Spooky
    Apr 8, 2015 at 8:59
1

The best I could find is to press F7 (step into) while in debug mode to have your constant line executed.

define('SOME_CONSTANT','VALUE');

And then double click "SOME_CONSTANT", right click and chose add new watch (shortcut: shift + ctrl + F7), and the add new watch window will appear with the constant prepopulated, select enter and you should see your constant value in the Variables (if you have the blue diamond selected "Show watches inside variables view") & Watches window panel.

Also quite handy to know, you can hover over variables to see their value as opposed to looking in the Debugging > Variables window panel...just need to turn it on as it's off by default...apparently buggy...using the latest xdebug and it's been fine for me so far.

Tools > Options > PHP > General Tab > Debugging Section > Check Watches and Balloon evaluation

Hopefully this will still be useful 2 years later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.