@reachmike at hotpop dot com:
First off the reason for the error showing dependant on error level is because references to undefined constants throw an error level of Notice, so you will need E_ALL or the inclusion of E_NOTICE in your error_reporting mode to see the error message.
As for the constant name TEST being output, in your code example you use <?= TEST ?> which is a shorthand for <?php echo TEST ?>, meaning you want to output the value of the constant on the page, however as the constant is not defined PHP is assuming you meant to use the string "TEST" as oppossed to a non-existant constant, and thus prints the string "TEST" onto the page.
You can easily solve your "just output a blank string if the constant doesn't exist" scenario using the constant() function:
<?= constant("TEST") ?>
This will work as you want, if the constant is defined, it's value will be output, otherwise an error level of E_WARNING will be thrown and a the constant name will not be output.
defined
(PHP 4, PHP 5)
defined — Checks whether a given named constant exists
Description
bool defined
( string $name
)
Checks whether the given constant exists and is defined.
Note: If you want to see if a variable exists, use isset() as defined() only applies to constants. If you want to see if a function exists, use function_exists().
Parameters
- name
-
The constant name.
Return Values
Returns TRUE if the named constant given by name has been defined, FALSE otherwise.
Examples
Example #1 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;
}
?>
defined
seth36 at gmail dot com
29-Dec-2008 05:29
29-Dec-2008 05:29
reachmike at hotpop dot com
01-Dec-2008 06:17
01-Dec-2008 06:17
You may find that if you use <?= ?> to dump your constants, and they are not defined, depending on your error reporting level, you may not display an error and, instead, just show the name of the constant. For example:
<?= TEST ?>
...may say TEST instead of an empty string like you might expect. The fix is a function like this:
<?php
function C(&$constant) {
$nPrev1 = error_reporting(E_ALL);
$sPrev2 = ini_set('display_errors', '0');
$sTest = defined($constant) ? 'defined' : 'not defined';
$oTest = (object) error_get_last();
error_reporting($nPrev1);
ini_set('display_errors', $sPrev2);
if ($oTest->message) {
return '';
} else {
return $constant;
}
}
?>
And so now you can do:
<?= C(TEST) ?>
If TEST was assigned with define(), then you'll receive the value. If not, then you'll receive an empty string.
Please post if you can do this in fewer lines of code or do something more optimal than toggling the error handler.
Anonymous
21-Oct-2008 03:36
21-Oct-2008 03:36
Nice one, here's may addition:
index.php:
<?php
// Main stuff here
define('SITE_IN', 1);
include "x.php";
?>
x.php:
<?php
if (!defined('SITE_IN') or !constant('SITE_IN')) die('Direct access not allowed!');
?>
A bit of extra protection, a bit paranoid yes, it's good to be :)
daniel at neville dot tk
14-Jul-2008 05:48
14-Jul-2008 05:48
My preferred way of checking if a constant is set, and if it isn't - setting it (could be used to set defaults in a file, where the user has already had the opportunity to set their own values in another.)
<?php
defined('CONSTANT') or define('CONSTANT', 'SomeDefaultValue');
?>
Dan.
admin at rune-city dot com
12-Jul-2008 11:39
12-Jul-2008 11:39
If you wish to protect files from direct access I normally use this:
index.php:
<?php
// Main stuff here
define('SITE_IN', 1);
include "x.php";
?>
x.php:
<?php
if (!defined('SITE_IN')) die('Direct access not allowed!');
?>
Kureal at kkooporation dot de
12-May-2008 08:46
12-May-2008 08:46
Use this at the top of your script:
<?
defined('Value') or die('Direct access to Script restricted.');
?>
and before including that file, use:
<?
define(Value, 1);
?>
I hope,
this is an short introduction to script access restriction,
Kenan Sulayman
Application Designer and CEO
KurealCorporation inc.
admin at baceto dot com
18-Apr-2008 09:57
18-Apr-2008 09:57
if (!defined("X")) {
echo "You Cannot Access This Script Directly, Have a Nice Day.";
exit();
}
This one is nice, actually for half an hour I was searching for an idea or a tip how to protect some files that must be accessed only if are included in another file ( that would admin.php). This one would perfectly do the trick.
Thanks a lot!
Shaun H
28-Mar-2008 07:30
28-Mar-2008 07:30
I saw that PHP doesn't have an enum function so I created my own. It's not necessary, but can come in handy from time to time.
<?php
function enum()
{
$args = func_get_args();
foreach($args as $key=>$arg)
{
if(defined($arg))
{
die('Redefinition of defined constant ' . $arg);
}
define($arg, $key);
}
}
enum('ONE','TWO','THREE');
echo ONE, ' ', TWO, ' ', THREE;
?>
Joel
20-Aug-2007 12:35
20-Aug-2007 12:35
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
18-May-2006 03:24
18-May-2006 03:24
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
ndove at cox dot net
28-Jan-2005 03:20
28-Jan-2005 03:20
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
Craig at chatspike dot net
30-Nov-2003 07:57
30-Nov-2003 07:57
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 :)
