Changes

Jump to: navigation, search

PHP Constants

786 bytes added, 14:20, 29 May 2007
Checking if a PHP Constant is Defined
== Checking if a PHP Constant is Defined ==
 
It can often be useful to find out if a constant is actually 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'' (i.e 1 or 0) to indicate whether that constant exists.
 
For example, let's assume we wish to find out if a constant named ''MY_CONSTANT'' is defined. We can simply call the ''defined()'' function passing through the name, and then test the result using an ''if .. else'' statement (see [[PHP Looping and Flow Control]] for more details on using ''if .. else''):
 
<pre>
<?php
define ('MY_CONSTANT', 36);
 
if (defined('MY_CONSTANT')
{
echo "Constant is defined";
}
else
{
echo "Constant ios not defined";
}
?>
</pre>

Navigation menu