note 73558 added to function.constant
| From: | vgr at europeanexperts dot org | Date: | Thu, 01 Mar 2007 13:57:12 +0000 |
| Subject: | note 73558 added to function.constant | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-123979@lists.php.net to get a copy of this message | ||
in reply to anonymous
[quote]
To check if a constant is boolean, use this instead:
<?php
if (TRACE === true) {}
?>
Much quicker and cleaner than using defined() and constant() to check for a simple boolean.
[/quote]
is definitely nor cleaner (because it's still as wrong as using simply "if (TRACE)")
nor quicker than " if (TRACE)" (one more comparison on a boolean value). This will
generate PHP errors. The constant TRACE is NOT defined.
error :
PHP Notice: Use of undefined constant TRACE - assumed 'TRACE' in
yourpath/test_constants.php on line 5
if you really want to be "clean" and as quick as possible, then there is a function :
[code]
function IsBooleanConstantAndTrue($constname) { // : Boolean
$res=FALSE;
if (defined($constname)) $res=(constant($constname)===TRUE);
return($res);
}
// use : if (IsBooleanConstantAndTrue('TRACE')) echo "trace is really
True<br>";
[/code]
If you want, you can see a demonstration at http://www.fecj.org/extra/test_constants.php
Regards
----
Server IP: 195.221.21.36
Probable Submitter: 82.243.20.181
----
Manual Page -- http://www.php.net/manual/en/function.constant.php
Edit -- https://master.php.net/note/edit/73558
Del: integrated -- https://master.php.net/note/delete/73558/integrated
Del: useless -- https://master.php.net/note/delete/73558/useless
Del: bad code -- https://master.php.net/note/delete/73558/bad+code
Del: spam -- https://master.php.net/note/delete/73558/spam
Del: non-english -- https://master.php.net/note/delete/73558/non-english
Del: in docs -- https://master.php.net/note/delete/73558/in+docs
Del: other reasons-- https://master.php.net/note/delete/73558
Reject -- https://master.php.net/note/reject/73558
Search -- https://master.php.net/manage/user-notes.php