PHP: Constants

Introduction to PHP Constants

Constants in PHP are identifiers for simple values that cannot be changed during the execution of a script. Unlike variables, constants are automatically global across the entire script.

Defining Constants

Constants are defined using the define() function or the const keyword.

Using define() function:


<?php
define("GREETING", "Welcome to PHP!");
echo GREETING;
?>
            

Using const keyword (PHP 5.3.0 and later):


<?php
const GREETING = "Welcome to PHP!";
echo GREETING;
?>
            

Note: The const keyword can be used to define constants at the top level of a script or inside a class definition, but not inside functions or loops.

Rules for PHP Constants

  • Constant names are case-sensitive by default. However, it's common practice to name constants in all uppercase letters.
  • Constants should start with a letter or underscore, followed by any number of letters, numbers, or underscores.
  • Once a constant is defined, it can't be changed or undefined.
  • Constants can only hold scalar values (boolean, integer, float and string) or arrays (PHP 7+).

Constants vs Variables

Here are the key differences between constants and variables:

  • Constants do not use the $ symbol before the identifier.
  • Constants can be defined and accessed anywhere in the script without regard to variable scoping rules.
  • Constants cannot be redefined or undefined once they have been set.
  • Constant values can only be strings and numbers (and arrays in PHP 7+).

Checking if a Constant is Defined

You can use the defined() function to check if a constant has been defined:


<?php
if (defined("GREETING")) {
    echo GREETING;
} else {
    echo "GREETING is not defined.";
}
?>
            

Magic Constants

PHP provides several predefined constants that change depending on where they are used. These are called magic constants:

  • __LINE__ - The current line number of the file.
  • __FILE__ - The full path and filename of the file.
  • __DIR__ - The directory of the file.
  • __FUNCTION__ - The function name (PHP 4.3.0 and higher).
  • __CLASS__ - The class name (PHP 4.3.0 and higher).
  • __METHOD__ - The class method name (PHP 5.0.0 and higher).
  • __NAMESPACE__ - The name of the current namespace (PHP 5.3.0 and higher).

Example:


<?php
echo "Line number " . __LINE__ . " in file " . __FILE__;
?>
            

Constant Arrays (PHP 7+)

From PHP 7.0, you can create an array constant using the define() function:


<?php
define("ANIMALS", [
    "dog",
    "cat",
    "bird"
]);
echo ANIMALS[1]; // outputs "cat"
?>
            

Best Practices

  • Use constants for values that don't change throughout your script, like configuration settings.
  • Name constants in all uppercase to distinguish them from variables.
  • Group related constants together, possibly in a separate configuration file.
  • Use constants instead of "magic numbers" or string literals in your code to improve readability and maintainability.

Conclusion

Constants are an essential part of PHP programming, providing a way to store values that remain unchanged throughout script execution. They enhance code readability, maintainability, and can help prevent accidental changes to important values in your applications.