Voting

: eight minus five?
(Example: nine)

The Note You're Voting On

warwick dot jm dot barnes at gmail dot com
2 years ago
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.

<< Back to user notes page

To Top