note 125119 added to language.constants
| From: | warwick dot jm dot barnes at gmail dot com | Date: | Tue, 16 Jun 2020 07:49:59 +0000 |
| Subject: | note 125119 added to language.constants | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-214769@lists.php.net to get a copy of this message | ||
The documentation says, "You can access constants anywhere in your script without regard to
scope", but it's worth keeping in mind that a const declaration must appear in the source
file before the place where it's used.
This doesn't work (using PHP 5.4):
<?php
foo();
const X = 1;
function foo() {
echo "Value of X: " . X;
}
?>
Result: "Value of X: X"
But this works:
<?php
const X = 1;
foo();
function foo() {
echo "Value of X: " . X;
}
?>
Result: "Value of X: 1"
This is potentially confusing because you can refer to a function that occurs later in your source
file, but not a constant. Even though the const declaration is processed at compile time, it behaves
a bit like it's being processed at run time.
----
Server IP: 45.112.84.4
Probable Submitter: 101.0.109.162 (proxied: 147.66.2.1)
----
Manual Page -- http://php.net/manual/en/language.constants.php
Edit -- https://master.php.net/note/edit/125119
Del: integrated -- https://master.php.net/note/delete/125119/integrated
Del: useless -- https://master.php.net/note/delete/125119/useless
Del: bad code -- https://master.php.net/note/delete/125119/bad+code
Del: spam -- https://master.php.net/note/delete/125119/spam
Del: non-english -- https://master.php.net/note/delete/125119/non-english
Del: in docs -- https://master.php.net/note/delete/125119/in+docs
Del: other reasons-- https://master.php.net/note/delete/125119
Reject -- https://master.php.net/note/reject/125119
Search -- https://master.php.net/manage/user-notes.php