How do I examine defined constants in PHP?

时光总嘲笑我的痴心妄想 提交于 2019-11-27 22:54:31

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) );

You probably want to adapt:

$arr = get_defined_vars();

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!