Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I examine defined constants in PHP?

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?

like image 900
MiseryIndex Avatar asked Dec 08 '09 01:12

MiseryIndex


People also ask

How do you check if constant is defined in PHP?

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.

How constants are defined in a PHP script?

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.

How is a constant defined in PHP by using the keyword?

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.

What are the ways to define a constant in PHP with example?

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.


2 Answers

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.

like image 180
MitMaro Avatar answered Oct 18 '22 03:10

MitMaro


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) );
like image 22
Spooky Avatar answered Oct 18 '22 04:10

Spooky