techmore.in

PHP - Constants

In PHP, constants are used to define values that do not change during the execution of the script. Unlike variables, once a constant is defined, it cannot be changed or undefined. Constants are particularly useful for values that need to remain the same throughout the execution, such as configuration settings.

Defining Constants

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

Using define():

The define() function takes two arguments: the name of the constant and its value.

php
<?php define("SITE_NAME", "Example.com"); define("MAX_USERS", 100); define("DEBUG_MODE", true); ?>

Using const:

The const keyword can also be used to define constants, but it only works within class definitions or at the top level.

php
<?php const SITE_NAME = "Example.com"; const MAX_USERS = 100; const DEBUG_MODE = true; ?>

Characteristics of Constants

  • Constants are automatically global and can be used across the entire script.
  • Constants do not start with a $ sign.
  • Constants can hold scalar data types such as boolean, integer, float, and string, as well as arrays (since PHP 5.6).

Examples

Basic Usage

php
<?php define("GREETING", "Welcome to Example.com!"); echo GREETING; // Outputs: Welcome to Example.com! ?>

Using Constants in Conditional Statements

php
<?php define("IS_ADMIN", true); if (IS_ADMIN) { echo "You have administrative privileges."; } else { echo "You are a regular user."; } ?>

Constants in Arrays (PHP 5.6+)

php
<?php define("FRUITS", ["Apple", "Banana", "Cherry"]); echo FRUITS[0]; // Outputs: Apple ?>

Constants with const

php
<?php const PI = 3.14159; const E = 2.71828; echo PI; // Outputs: 3.14159 echo E; // Outputs: 2.71828 ?>

Constants in Classes

Constants can also be defined within classes using the const keyword.

php
<?php class MyClass { const CONSTANT_VALUE = "This is a constant value"; public function showConstant() { echo self::CONSTANT_VALUE; } } $instance = new MyClass(); $instance->showConstant(); // Outputs: This is a constant value echo MyClass::CONSTANT_VALUE; // Outputs: This is a constant value ?>

Magic Constants

PHP provides several predefined constants, known as magic constants, which change depending on where they are used. Some common magic constants are:

  • __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.
  • __CLASS__ : The class name.
  • __METHOD__ : The class method name.
  • __NAMESPACE__ : The current namespace name.

Example of Magic Constants

php
<?php echo "This is line " . __LINE__ . "<br>"; echo "The file path is " . __FILE__ . "<br>"; echo "The directory is " . __DIR__ . "<br>"; function myFunction() { echo "The function name is " . __FUNCTION__ . "<br>"; } myFunction(); class MyClass { public function myMethod() { echo "The class name is " . __CLASS__ . "<br>"; echo "The method name is " . __METHOD__ . "<br>"; } } $instance = new MyClass(); $instance->myMethod(); ?>

Summary

  • Defining Constants:
    • Using define(): define("CONSTANT_NAME", value);
    • Using const: const CONSTANT_NAME = value;
  • Characteristics:
    • Constants are global and can be accessed anywhere.
    • They do not start with a $ sign.
    • They cannot be changed once defined.
  • Usage:
    • Constants are useful for values that should remain the same throughout the execution.
    • Magic constants provide useful information about the script's context.

Using constants effectively can help make your code more readable and maintainable by providing a clear way to define and use fixed values throughout your application.