YouTip LogoYouTip

Php Constants

# PHP Constants * * * In PHP, constants are identifiers whose value cannot be changed once defined. Once a constant value is defined, it cannot be changed anywhere else in the script. Constants can be defined using the `define()` function or the `const` keyword. ### Characteristics of Constants * * * ## PHP Constants 1. **Immutability**: Once a constant is defined, its value cannot be changed. 2. **Global Scope**: After definition, constants can be used anywhere in the script without using the `global` keyword. 3. **Data Types**: The value of a constant can be a scalar data type (such as boolean, integer, float, string) or an array (PHP 7 and above). 4. **Case Sensitivity**: Constant names are case-sensitive by default. To define a case-insensitive constant, you can set the third parameter of the `define()` function to `true`. A constant is an identifier for a simple value that cannot change during the script's execution. A constant consists of letters, underscores, and numbers, but the number cannot be the first character. (Constant names do not need the `$` modifier). **Note:** Constants can be used anywhere in the script. * * * ## Setting PHP Constants To set a constant, use the `define()` function. The function syntax is: bool define ( string $name , mixed $value [, bool $case_insensitive = false ] ) The function has three parameters: * **name:** Required parameter, the name of the constant, i.e., the identifier. * **value:** Required parameter, the value of the constant. * **case_insensitive:** Optional parameter. If set to `TRUE`, the constant is case-insensitive. By default, it is case-sensitive. **Note:** Since PHP 7.3.0, defining case-insensitive constants has been deprecated. From PHP 8.0.0, only `false` is an acceptable value; passing `true` will generate a warning. In the following example, we create a **case-sensitive constant** (not recommended after PHP 7.3), with the value "Welcome to .com": ## Example <?php// Case-sensitive constant name define("GREETING", "Welcome to .com"); echo GREETING; // Outputs "Welcome to .com"echo'
'; echo greeting; // Outputs "greeting", but with a warning indicating the constant is not defined?> In the following example, we create a **case-insensitive constant**, with the value "Welcome to .com": ## Example * * * ## Constants are Global Constants are global by default after definition and can be used anywhere in the running script. The following example demonstrates using a constant inside a function, even though the constant is defined outside the function. ## Example * * * ## Using the const Keyword const CONSTANT_NAME = "value"; Here is an example of defining a constant using the `const` keyword: ## Example const SITE_URL =""; echo SITE_URL;// Outputs "" * * * ## Predefined Constants PHP provides some predefined constants that can be used directly in scripts. These constants are typically used to obtain PHP configuration information, version information, etc. Common predefined constants include: * `PHP_VERSION`: The version of the current PHP parser. * `PHP_OS`: The operating system of the server. * `PHP_INT_MAX`: The largest integer value. * `E_ERROR`, `E_WARNING`, `E_PARSE`, etc.: Error reporting levels. ## Example echo PHP_VERSION;// Outputs the PHP version, e.g., "7.4.1" echo PHP_OS;// Outputs the operating system, e.g., "Linux" echo PHP_INT_MAX;// Outputs the largest integer value, e.g., "9223372036854775807" * * * ## Constant Arrays (PHP 7 and above) In PHP 7 and above, constants can also be arrays. ## Example define("FRUITS",[ "Apple", "Banana", "Orange" ]); echo FRUITS;// Outputs "Apple" Or using `const`: ## Example const COLORS =[ "Red", "Green", "Blue" ]; echo COLORS;// Outputs "Green"
← Bootstrap GlyphiconsPhp Datatypes β†’