Search⌘ K
AI Features

Constants

Explore PHP constants and their use as immutable variables that remain unchanged during script execution. Learn to define constants using const and define, check for their existence, access reserved names, and retrieve all defined constants to write reliable PHP code.

Defining Constants

Constants, by definition, are variables that cannot be modified during the execution of the script. They are created using the const statement or the define function.

Convention: In PHP, the convention is to use uppercase letters for constant names, e.g., A_CONSTANT.

The following code snippet shows how constants are created: ...

PHP
<?php
const PI = 3.14; // float
define("EARTH_IS_FLAT", false); // boolean
const UNKNOWN = null; // null
define("APP_ENV", "dev"); // string
const MAX_SESSION_TIME = 60 * 60; // integer, using (scalar) expressions is ok
const APP_LANGUAGES = ["de", "en"]; // arrays
define("BETTER_APP_LANGUAGES", ["lu", "de"]); // arrays
?>

If you ...