PHP Constants


In this article, you will learn about PHP constants, how they are created and their use in applications.

php constants

What is Constant in PHP?
Differences between constants and variables
Valid and invalid constant names
PHP Magic Constants

PHP Constants: Definition

What is Constant in PHP?

A constant is a name or an identifier for a fixed value. Constants are like variables, except that once they are defined, they cannot be undefined or changed (except magic constants).

Valid constants name starts with a letter or underscores without a $ sign before its name. If you have defined a constant, it can never be changed or undefined.

Constants are very useful for storing data that doesn’t change while the script is running. Common examples of such data include configuration settings such as database username and password, website’s base URL, company name, etc.

How to define a PHP constant?

To create constants, use the define() function. As indicated by the name, this function will return the value of the constant.       

Syntax

define(name, value, case-insensitive)

Here, the name is the identifier for the constant which can be used anywhere in the script, value is the content that it will hold and case-insensitive specifies whether or not the constant will be case sensitive or not. By default, it is false.

<?php

   define("max", 100);

   echo max; //this will output 100

?>

PHP Constants vs PHP Variables

  • Constants can’t be changed

The primary difference between a PHP constant and a PHP variable is that you can change the value of a static variable, but you can’t with the constants. 

  • Naming convention

There is no need to write a dollar sign ($) before a constant, whereas while declaring variable one has to write a dollar sign. 

  • Constants are global

Constants may be defined and accessed anywhere without regard to variable scoping rules. Constants are automatically global and can be used across the entire script.

 

Valid and invalid PHP constant names

// Valid constant name

define("ONE",     "first thing");

define("TWO2",    "second thing");

define("THREE_3", "third thing");

 

// Invalid constants name

define("2TWO",    "second thing");

define("__THREE__", "third value");

PHP Magic Constants

PHP provides a large number of predefined constants to any script which it runs.

There are five magical constants that change depending on where they are used. 

_LINE__

The __LINE__ t returns the current line number of the file, like this:

Example:

<?php

echo "Line number " . __LINE__ . "<br>"; 

number " . __LINE__ . "<br>"; __LINE__ . "<br>"; 

 ?>

Output:

Line number 2

Line number 3

Line number 4

__FILE__

It returns full path and filename of the file. If used inside an include, the name of the included file is returned.

Example:

<?php

 echo "The full path of this file is: " . __FILE__;

 ?>

Output:

// Displays the absolute path of this file

__FUNCTION__

The __FUNCTION__ constant returns the name of the current function.

Example:

<?php

 function myFunction(){

 echo "The function name is - " . __FUNCTION__; }

myFunction();

 ?>

Output:

The function name is – myFunction

__CLASS__

It returns the class name. (Added in PHP 4.3.0) As of PHP 5 these constants return the class name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.

Example:

<?php

class MyClass

{

    public function getClassName(){

        return __CLASS__;

    }

}

$obj = new MyClass();

echo $obj->getClassName();

?>

Output:

MyClass

__METHOD__

It returns the class method name. (Added in PHP 5.0.0) The method name is returned as it was declared (case-sensitive).

Example:

<?php

class Sample

{

    public function myMethod(){

        echo __METHOD__;

    }

}

$obj = new Sample();

$obj->myMethod();

?>

Output

Sample::myMethod