A constant is a named value that, unlike a variable, can never be changed once it's set. Use one whenever a value genuinely should never change while the script runs — a maximum file size, a tax rate, a site name.
<?php
define("SITE_NAME", "Learn Computer Academy");
echo SITE_NAME;
?>Notice constants don't use the $ sigil, and by convention are written in UPPER_SNAKE_CASE to make them visually stand out from ordinary variables at a glance.
The const keyword does the same job, with slightly different rules — it can only be used at the top level of a file or inside a class (you'll meet classes later in this section), and its value must be known at compile time rather than computed:
<?php
const MAX_UPLOAD_SIZE = 5242880; // 5 MB, in bytes
echo MAX_UPLOAD_SIZE;
?><?php
define("PI", 3.14159);
// define("PI", 3.14); // Fatal error — PI is already defined
?>Trying to redefine a constant is a fatal error, not a warning — PHP stops the script entirely. That's the whole point of a constant: once set, it's a guarantee, not a suggestion.
const vs define() in practice
In everyday PHP code, const is more common than define() for values known ahead of time, since it's slightly faster and reads more consistently with other languages. define() is still useful when a constant's value needs to be computed conditionally, which const doesn't allow.