PHP › Constants & Superglobals
What is a constant in PHP, and how does it differ from a regular variable?
BeginnerAnswer
A constant is a named value that, once defined, cannot be changed for the rest of the script's execution, unlike a regular variable whose value can be reassigned at any time, and constants are typically used for values that should remain fixed throughout an application, such as a configuration setting or a mathematical value, making the code clearer about the fact that this value is never expected to change.
define('MAX_LOGIN_ATTEMPTS', 5);
echo MAX_LOGIN_ATTEMPTS;
const APP_VERSION = '1.0.0';Real-world example A login system defines a constant for the maximum number of allowed failed login attempts, making the value clearly visible and impossible to accidentally overwrite anywhere else in the application's code.