Global PHP Constants | PHP Gurukul

What is PHP Constants?

Constants are variables
Constants are variables except that once they are defined they can’t be changed or undefined.

  • A constant is an identifier (name) for a simple value. The value cannot be changed during the script.
  • A valid constant name starts with a letter or underscore.
  • (no $ sign before the constant name).
  • Note: Unlike variables, constants are automatically global across the entire script.
How to create a PHP constant
  • You can create a constant by using the define () function.
  • Use the define function to specify the name and value of a constant. (name, value, case-insensitive).
  • Many programmers use capital letters to name constants – so they stand out.
  • Note: Since a constant can not be change you do not use a $ sign before it’s name when you declare it or (use it).

name: Specifies the name of the constant
value: Specifies the value of the constant
case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false

How to declare constants


PHP Constant Examples

Output: Welcome to!

Click :

Difference Between Constant and Variables
  • The mentioning of a dollar sign($) is the first evident difference between constants and variables. Variables have to be preceded by a dollar sign while constants are specified without it.
  • Variables are assigned values using the simple assignment. However, constants cannot be defined in this manner. Constants can only be defined using define() function.
  • Variables can be assigned, reassigned and un-defined. How-ever, constant cannot be modified in any way during the course of program.
  • The Variable scoping rules do not apply to contants. They can be accessed and defined anywhere
Magic Constants

There are predefined constants supported by PHP. Any script that runs on PHP can access these constants. Some constants in PHP change on the basis of where they are being used. For instance, the constant ___LINE___ is dependent on the line that is being used. These are case-insensitive. Some of the PHP constants, where are defined as ‘magical’ are as follows-

Magic Constant Meaning
___LINE___ This constant indicates the present line number.
___FILE___ This constant contains the path and file name of the file concerned. It is important to mention here that the path contained by this constant is the absolute path.
___FUNCTION___ This constant includes the name of the function as it was when the function was declared. The name is case-sensitive
___CLASS___ This constant returns the name of the class concerned in a case-sensitive manner and exactly in the manner that it was declared.
___METHOD___ This constant returns the name of the method concerned in a case-sensitive manner and exactly in the manner that it was declared.
Blog :


PHP Gurukul

Welcome to PHPGurukul. We are a web development team striving our best to provide you with an unusual experience with PHP. Some technologies never fade, and PHP is one of them. From the time it has been introduced, the demand for PHP Projects and PHP developers is growing since 1994. We are here to make your PHP journey more exciting and useful.

Website :

Showing 1 reaction

Please check your e-mail for a link to activate your account.
  • php gurukul
    published this page in Submit Content 2022-10-01 10:47:04 -0700