How to check if a defined constant exists in PHP? How to check if a defined constant exists in PHP? php php

How to check if a defined constant exists in PHP?


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.


Use defined() function, for example:

if (defined('VAR_NAME')) {    // Something}


Check using defined('CONSTANT') function.

An example from the manual:

<?php/* Note the use of quotes, this is important.  This example is checking * if the string 'TEST' is the name of a constant named TEST */if (defined('TEST')) {    echo TEST;}?>