Declare PHP Constants and Read Them Alongside Variables in Functions
Learn when to use a PHP variable or constant, how to declare each, and how to read their values inside a function.
If a value needs to change, put it in a $variable. If it must stay fixed after declaration, use a constant and read its name without $. A function can read the constant directly; in this example, global $stage makes the outside variable available inside the function. That difference matters when you want a function to use a shared label without accidentally treating it as a changeable value.
Choose a variable for a changing value and a constant for a fixed value
Suppose a page has a publication stage that moves from Draft to Published, while its site name and topic label stay the same. The stage belongs in a variable because the code reassigns it. The labels are suitable constants because they are intended to retain their defined values.
This is a choice about whether a value may change, not about whether it contains text. Both the stage and the labels in the example below are strings. PHP’s assignment operator stores the value on the right in the variable on the left, so the second assignment to $stage replaces the value the example will print. Once a constant is defined, it cannot be changed or undefined, according to the PHP manual.
Declare and access each form
Here is one complete example. It assigns and reassigns $stage, declares SITE_NAME with const, and declares TOPIC_LABEL with define(). The function then reads those same three values. The <?php tag makes the snippet a PHP file rather than an unmarked fragment.
<?php
$stage = 'Draft';
$stage = 'Published';
const SITE_NAME = 'Guilty Of PHP';
define('TOPIC_LABEL', 'Constants');
echo $stage;
echo "\n";
echo SITE_NAME;
echo "\n";
echo TOPIC_LABEL;
echo "\n";
function showValues() {
global $stage;
echo SITE_NAME;
echo "\n";
echo TOPIC_LABEL;
echo "\n";
echo $stage;
echo "\n";
}
showValues();
The expected output is:
Published
Guilty Of PHP
Constants
Guilty Of PHP
Constants
Published
Notice the punctuation in the echo statements: $stage has a dollar sign, while SITE_NAME and TOPIC_LABEL do not. The quotes around 'TOPIC_LABEL' in define() supply its name to the declaration; when reading that constant, the code writes TOPIC_LABEL without quotes or $. The first three lines of output come from the statements outside the function. The last three come from showValues().
Read the values inside a function
Inside showValues(), neither constant needs a global statement. Constants can be accessed without regard to variable scoping rules; the manual’s constant syntax page also makes the no-$ distinction explicit. By contrast, the function declares global $stage before reading the variable assigned outside it.
Follow the values rather than just their names. The code first puts Draft in $stage, then replaces it with Published. Both reads of $stage therefore appear as Published in the expected output. SITE_NAME and TOPIC_LABEL retain the values supplied at their declarations. There is no attempted constant reassignment hidden in the function: reading a fixed value and changing it are different operations.
Check where each declaration is allowed
Keep a plain const declaration at top-level scope. It cannot be placed inside this example’s function—or inside an if statement, loop, or try/catch block—because such constants are defined at compile time. The PHP manual distinguishes that rule from define().
If a constant must be defined when execution reaches a function or condition, call define() there instead. The example uses define() at top level to show its syntax, but unlike const, the call can also occur inside a function or condition. That flexibility concerns where and when the declaration happens; it does not make the resulting constant changeable. Do not try to reassign either constant as a way to update it. If you are considering defining the same name again, note the version boundary: redeclaring an already-defined constant is deprecated as of PHP 8.5.0, according to the PHP manual.
