PHP Constants
PHP Constants
Section titled “PHP Constants”Constants are identifiers for values that do not change during the execution of a script. Once defined, their value remains fixed.
Understanding PHP Constants
Section titled “Understanding PHP Constants”A constant is a name (identifier) for a simple value. Unlike variables, the value assigned to a constant cannot be altered after it’s defined.
A valid constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. By convention, constant names are usually written in uppercase letters. Importantly, constant names do not have a preceding dollar sign ($) like variables.
Note: Constants have a global scope by default, meaning they can be accessed from anywhere in the script without needing the global keyword.
Creating PHP Constants
Section titled “Creating PHP Constants”There are two primary ways to define constants in PHP:
- Using the
define()function: This is the traditional way and allows defining constants conditionally or with names determined at runtime. - Using the
constkeyword: Introduced later, this method is generally preferred for defining constants at the top level of a script or within classes, as it’s slightly faster and clearer for compile-time constants.
Using define()
Section titled “Using define()”The define() function creates a named constant.
Syntax:
define(string $name, mixed $value)Parameters:
- name: The name of the constant (case-sensitive by default).
- value: The value of the constant. Can be scalar values (int, float, string, bool, null) or arrays (since PHP 7).
Note: The third parameter, case-insensitive, was available in older PHP versions but is deprecated as of PHP 7.3 and removed in PHP 8.0. Constants are now always case-sensitive.
Example using define():
<?php
define("GREETING", "Welcome to Modern PHP!");echo GREETING; // Output: Welcome to Modern PHP!
// Defining an array constant (PHP 7+)define('ALLOWED_LANGUAGES', ['en', 'es', 'fr']);print_r(ALLOWED_LANGUAGES);
?>Using const
Section titled “Using const”The const keyword provides a simpler syntax for defining constants, especially at the global scope or within classes.
Syntax (global scope):
const CONSTANT_NAME = value;Example using const:
<?php
const APP_VERSION = '2.1.0';echo APP_VERSION; // Output: 2.1.0
const DEFAULT_SETTINGS = [ 'theme' => 'dark', 'notifications' => true];
echo DEFAULT_SETTINGS['theme']; // Output: dark
?>Constants defined with const must be declared at the top-level scope or within classes; they cannot be defined inside functions or control structures like define() can.
Constants are Global
Section titled “Constants are Global”Constants are automatically global and accessible throughout the script, regardless of where they were defined.
Example showing global scope:
<?php
const SITE_NAME = "My Awesome Website";
function displaySiteName() { // SITE_NAME is accessible inside the function echo "Welcome to " . SITE_NAME;}
displaySiteName(); // Output: Welcome to My Awesome Website
?>Magic Constants
Section titled “Magic Constants”PHP provides a number of predefined ‘magic’ constants that change depending on where they are used. These are useful for debugging and obtaining meta-information about the script.
__LINE__: The current line number in the file.__FILE__: The full path and filename of the file.__DIR__: The directory of the file.__FUNCTION__: The function name.__CLASS__: The class name.__TRAIT__: The trait name.__METHOD__: The class method name.__NAMESPACE__: The name of the current namespace.ClassName::class: The fully qualified class name (available since PHP 5.5).
Example using __DIR__:
<?php
// Imagine this code is in /var/www/myapp/config/app.phpecho "Configuration directory: " . __DIR__;// Output: Configuration directory: /var/www/myapp/config
?>Further Reading:
Section titled “Further Reading:”For more details, refer to the official PHP documentation on Constants: