defined() In Action

../_images/defined.png

The defined() function checks if a constant is defined or not.

It is possible to check the constant with its name, and with its namespace.

Beware of quotes: most often, when checking for the constant’s value, the result is false.

On the other hand, it is not possible to check a constant with its aliased name: extra names, created with use are not validated.

To check for a class constant, it is possible to use a string, with the class name and the :: separator. Then, the visibility of the class constant is taken into account: it might be defined, but not accessible from that part of the code.

See Also

PHP Features