How to check if a defined constant exists in PHP?

ryanc1256 picture ryanc1256 · Jan 20, 2013 · Viewed 83.4k times · Source

So I'm using a PHP framework called fuelphp, and I have this page that is an HTML file, so I can't use PHP in it. I have another file that has a top bar in it, which my HTML file will call through ajax.

How do I check if a constant exists in PHP?
I want to check for the the fuelphp framework file locations.

These are the constants I need to check for (actually, I only have to check one of them):

define('DOCROOT', __DIR__.DIRECTORY_SEPARATOR);
    define('APPPATH', realpath(__DIR__.'/fuel/app/').DIRECTORY_SEPARATOR);
    define('PKGPATH', realpath(__DIR__.'/fuel/packages/').DIRECTORY_SEPARATOR);
    define('COREPATH', realpath(__DIR__.'/fuel/core/').DIRECTORY_SEPARATOR);                    
    require APPPATH.'bootstrap.php';

edit:
I realized that these aren't variables they are constants...

Answer

Eric MORAND picture Eric MORAND · Jan 20, 2013

First, these are not variables, but constants.

And you can check their existence by using the defined() function :

bool defined ( string $name )

Checks whether the given constant exists and is defined.