note 118671 added to language.constants
| From: | gried at NOSPAM dot nsys dot by | Date: | Sun, 17 Jan 2016 19:12:40 +0000 |
| Subject: | note 118671 added to language.constants | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-204704@lists.php.net to get a copy of this message | ||
Lets expand comment of 'storm' about usage of undefined constants. His claim that 'An
undefined constant evaluates as true...' is wrong and right at same time. As said further in
documentation ' If you use an undefined constant, PHP assumes that you mean the name of the
constant itself, just as if you called it as a string...'. So yeah, undefined global constant
when accessed directly will be resolved as string equal to name of sought constant (as thought PHP
supposes that programmer had forgot apostrophes and autofixes it) and non-zero non-empty string
converts to True.
There are two ways to prevent this:
1. always use function constant('CONST_NAME') to get constant value (BTW it also works for
class constants - constant('CLASS_NAME::CONST_NAME') );
2. use only class constants (that are defined inside of class using keyword const) because they are
not converted to string when not found but throw exception instead (Fatal error: Undefined class
constant).
----
Server IP: 72.52.91.14
Probable Submitter: 93.85.29.7
----
Manual Page -- http://php.net/manual/en/language.constants.php
Edit -- https://master.php.net/note/edit/118671
Del: integrated -- https://master.php.net/note/delete/118671/integrated
Del: useless -- https://master.php.net/note/delete/118671/useless
Del: bad code -- https://master.php.net/note/delete/118671/bad+code
Del: spam -- https://master.php.net/note/delete/118671/spam
Del: non-english -- https://master.php.net/note/delete/118671/non-english
Del: in docs -- https://master.php.net/note/delete/118671/in+docs
Del: other reasons-- https://master.php.net/note/delete/118671
Reject -- https://master.php.net/note/reject/118671
Search -- https://master.php.net/manage/user-notes.php