note 87340 added to function.defined
| From: | reachmike at hotpop dot com | Date: | Mon, 01 Dec 2008 06:17:50 +0000 |
| Subject: | note 87340 added to function.defined | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-147473@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 64.71.164.2
Probable Submitter: 72.71.64.29
----
Manual Page -- http://www.php.net/manual/en/function.defined.php
Edit -- https://master.php.net/note/edit/87340
Del: integrated -- https://master.php.net/note/delete/87340/integrated
Del: useless -- https://master.php.net/note/delete/87340/useless
Del: bad code -- https://master.php.net/note/delete/87340/bad+code
Del: spam -- https://master.php.net/note/delete/87340/spam
Del: non-english -- https://master.php.net/note/delete/87340/non-english
Del: in docs -- https://master.php.net/note/delete/87340/in+docs
Del: other reasons-- https://master.php.net/note/delete/87340
Reject -- https://master.php.net/note/reject/87340
Search -- https://master.php.net/manage/user-notes.php