note 125119 deleted from language.constants by cmb
| From: | cmb@php.net | Date: | Tue, 16 Jun 2020 08:00:56 +0000 |
| Subject: | note 125119 deleted from language.constants by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-214771@lists.php.net to get a copy of this message | ||
Note Submitter: warwick dot jm dot barnes at gmail dot com
----
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.