Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Miscellaneous Functions : defined

defined

Checks whether a given named constant exists (PHP 4, PHP 5)
bool defined ( string name )

Example 1353. Checking Constants

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

Related Examples ( Source code ) » defined







Code Examples / Notes » defined

craig

This can be useful if you want to protect pages which get included from outsiders eyes, on your mail page (the page viewable by people) put define("X", null); then on all your other pages, you can then do something like:
if (!defined("X")) {
echo "You Cannot Access This Script Directly, Have a Nice Day.";
exit();
}
And your page is a good as protected :)


ndove

In PHP5, you can actually use defined() to see if an object constant has been defined, like so:
<?php
class Generic
{
   const WhatAmI = 'Generic';
}
if (defined('Generic::WhatAmI'))
{
   echo Generic::WhatAmI;
}
?>
Thought it may be useful to note.
-Nick


joel

If your constants don't show up in your included or required files, then you probably have php safe mode turned on!
I ran into this problem, I forgot to turn of safe mode when I was creating a new site.


harald ponce de leon

Beware that some PHP versions return an integer (1 or 0) instead of a boolean.
Confirmed PHP versions that return an integer are 4.3.2 and 4.3.4.
Relevant bug report:
http://bugs.php.net/bug.php?id=27443
This make it impossible to use the following, when the PHP version is not known:
if (defined('CONSTANT') === true) {
}
Relevant commit for PHP 4.3.5 (thanks to Pollita at #php.thinktank):
http://cvs.php.net/viewcvs.cgi/Zend/zend_builtin_functions.c?
r1=1.124.2.13&r2=1.124.2.14


Change Language


Follow Navioo On Twitter
connection_aborted
connection_status
connection_timeout
constant
define
defined
die
eval
exit
get_browser
__halt_compiler
highlight_file
highlight_string
ignore_user_abort
pack
php_check_syntax
php_strip_whitespace
show_source
sleep
sys_getloadavg
time_nanosleep
time_sleep_until
uniqid
unpack
usleep
eXTReMe Tracker