|
defined
Checks whether a given named constant exists
(PHP 4, PHP 5)
Example 1353. Checking Constants<?php Related Examples ( Source code ) » defined Examples ( Source code ) » Class information Examples ( Source code ) » Class method info is User Defined, is Internal, is Abstract Examples ( Source code ) » Use static variable defined in function Examples ( Source code ) » Use variable defined outside function Examples ( Source code ) » Variables Defined Outside Functions Are Inaccessible from Within a Function by Default Examples ( Source code ) » Animated Talking Captcha php class Code Examples / Notes » definedcraig
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 |