A constant is an identifier (name) for a value that cannot be changed during the script's execution. Constants do not start with a $ and are case-sensitive by default.
define() is a function used to create constants:
define("SITE_NAME", "MySite");
echo SITE_NAME;const is used to define constants in the global scope or inside classes:
const PI = 3.14159;
echo PI;Inside a class:
class Math {
const VERSION = "1.0";
}
echo Math::VERSION;You can access a constant from anywhere in the script, even inside functions:
define("APP_NAME", "PHP Demo");
function showApp() {
echo APP_NAME;
}PHP provides many built-in constants:
PHP_VERSION — current PHP versionPHP_OS — operating system PHP is running onPHP_INT_MAX — largest integer supported__LINE__, __FILE__, __DIR__ — magic constantsecho __FILE__;
echo PHP_VERSION;$Ask the AI if you need help understanding or want to dive deeper in any topic