That would go against the whole point of a variable
The whole purpose of a variable is that it is variable in value, it can change later in the program.
What can I use instead?
But you could use a constant:
// Works on PHP 5.3.0 and later
const SOME_CONSTANT = "Some value.";
OR
// Far more well known, works on older PHPs, does the same thing
define("SOME_CONSTANT", "Some value");
How do I use constants?
Then use it much like you would use a variable:
echo SOME_CONSTANT;
You can use a constant in nearly any way in which you can use a variable, just traditionally you'll use an upper-case name for the constant (this isn't enforced in PHP) and it wont start with a $
symbol (this is enforced)... oh and of course, you can't re-assign it.
Note: Because constants defined using the const
keyword are defined at compile time, you CANNOT use this syntax inside loop structures, if
statements, switch
statements etc, and must use define()
instead.