PHP Constants

Last Updated : May 8, 2021

PHP Constants are non-volatile identifier or a name to store data. Unlike variables in PHP, we cannot modify a Constant value once they are defined. PHP Constants has global scope by default and can be used anywhere in the PHP program, including Functions, Classes, etc.

Naming Convention of PHP Constants

  • A Constant name should start with alphabets and underscore.
  • A Constant name should not start with $ symbol like Variables.
  • A Constant cannot be changed once defined
  • A Constant is a global scope identifier by default

How to create a PHP Constant

define(name, value, case-insensitive=TRUE|FALSE)

or

const CONSTANT_NAME

PHP Constant can be created using define() function and const keyword. By default constant is case-sensitive but by sending the third parameter’s value as true we can make it case-insensitive. Also, when we use const keyword to create a Constant, it’s case-sensitive as always. Please find below the examples,

<?php

// using define() method
define('SITE_NAME', "PHP Tutorials", true);
echo SITE_NAME .'<br>';
echo site_name .'<br>';

// using const keyword
const SITE_AUTHOR = "Admin";
echo SITE_AUTHOR .'<br>';
echo site_author;
?>

To retrieve the Constant, we can use constant() method, based on the key we passed into this function, it will return the corresponding value.

<?php
constant('SITE_NAME'); // returns "PHP Tutorials"
constant('SITE_AUTHOR'); // returns "Admin"
?>

PHP Majic Constants

PHP Majic Constants are predefined constants that return the dynamic value based on where it has been used/called at the moment. It usually starts with two underscores at the beginning and the end like __NAME__. Please find below a list of majic constants available in PHP.

NameDescription
__LINE__It returns the current line of the file.
__FILE__It returns the full path along with the name of the file.
__DIR__It returns the directory of the file.
__FUNCTION__It will return the name of the current function if it is called inside from the function.
__CLASS__It returns the name of the current Class.
__TRAIT__It returns the name of the traits, including Namespace.
__METHOD__It returns the name of the current Class Method.
__NAMESPACE__It returns the name of the current Namespace.

When to use PHP Constants?

  • If we are going to use certain Variables in multiple places across the scope of PHP, then we can declare PHP Constants.
  • When we use MySql in PHP, we should use the database’s username/password in many places. In that scenario, we can assign these values into Constants and use them in the PHP program. If we want to change those credentials, we can change them in one place, and it reflects where ever we used those Constants.
  • Usually, in many PHP Frameworks and WordPress CMS, we will use Constants for the global configurations. These configuration values will be used all over the project.

In this tutorial, we learned about PHP Constants and how to use them effectively, let’s jump into next chapter of this tutorial series.