Updated March 17, 2023
Introduction to PHP Constants
PHP Constants are variables whose values, once defined, cannot be changed, and these constants are defined without a $ sign in the beginning. PHP Constants are created using define() function. This function takes two parameters first is the name, and the second is the value of the constant defined.
The name of the constant starts using letters or underscores and not with a number. It can start with a letter or underscore followed by letters, underscores or numbers. The name is case-sensitive and in uppercase. After a constant is defined, it cannot be undefined or redefined again. It remains the same throughout the script and cannot be changed as the variables do.
Syntax with Explanation
A constant is a name for a particular value. To define a constant, we have to use the define() function and to get the value of the constant; we just need to specify the name.
Syntax:
define(name, value, case-insensitive);
where name is the name of the constant,
value is the value of the constant,
case-insensitive is either true or false, by default, it is false.
define('TEXT', 'Hello World!');
A constant can also be defined using const construct.
<?php
const MSG = "WELCOME";
echo MSG;
?>
How to Create Constants in PHP using Various Methods?
To create constants, we have to use a simple define function, which takes two parameters, first the name of the constant second the value to be stored. The name is by default in uppercase. It does not start with a $.
Example #1
Code:
<?php
//example to demonstrate constants
define("TEXT", "Hello World!");
echo TEXT;
?>
Output:
In this example, we will be using a const construct to define a constant named TEXT. We have used const followed by the name of the constant and then the value. It can be assigned a value using an assignment operator =.
Once we have defined the constant, to access the defined constant TEXT, we will echo the name with the constant keyword, as shown below.
Example #2
Code:
<?php
// program to demonstrate in PHP 7 using const keyword
const TEXT = 'PHP PROGRAMMING!';
echo TEXT;
echo constant("TEXT");
?>
Output:
Example #3
In the below example, we are defining a TEXT constant with a value. Also, in the same program, we have defined a function Demo(). We have declared the TEXT constant outside the function Demo. Here we see that we can access the constant TEXT from within the function. This means once you define the constant, it is globally available in the script.
Code:
<?php
//example to demonstrate the define constants globally
define("TEXT", "Hello World!");
echo TEXT;
function Demo() {
echo '<br>';
echo TEXT;
}
Demo();
?>
Output :
Rules and Regulations for PHP Constants
The following are the rules to define PHP constants.
- should not start with a $.
- should not start with a number.
- should not start with an underscore.
- start with a letter and follow by numbers.
- start with a letter and follow by an underscore and numbers.
Let us look at the below statements.
<?php
define("TEXT","PHP"); //valid
define("TEXT1", "PHP"); //valid
define("1TEXT", "PHP"); //invalid
define("1_TEXT", "PHP"); //invalid
define("TEXT_1", "PHP"); //valid
define("__TEXT__", "PHP"); // valid but should be avoided
?>
Magic Constants
It starts with a double underscore
- __LINE__
- __FILE__
- __FUNCTION__
- __CLASS__
- __METHOD__
1. __LINE__
This gives the current line number.
Code:
<?php
//example to demonstrate PHP magic constant __LINE__
echo 'I am at Line number '. __LINE__;
?>
Output:
2.__FILE__
This gives the filename along with the file path of the file. It can be used to include a file in a script.
Code:
<?php
//example to demonstrate PHP magic constant __FILE__
echo 'FILE NAME '. __FILE__;
?>
Output:
3. __FUNCTION__
This gives the name of the function in which it is declared. It is case-sensitive.
Code:
<?php
// example to demonstrate the magic constant __FUNCTION__
function show() {
echo 'In the function '.__FUNCTION__;
}
show();
?>
Output:
4. __METHOD__ , __CLASS__
This gives the name of the method and the name of the class in which it is declared. In the below example, we have defined the MainClass and two methods within it, the show method and the test method. Inside the show method, we have printed the __CLASS__, which gives the class name and inside the test method, we have printed the __METHOD__, which gives the method name, test.
Code:
<?php
// example to demonstrate the magic constant __CLASS__ and __METHOD__
class MainClass
{
function show() {
echo "<br>".__CLASS__;
}
function test() {
echo "<br>".__METHOD__;
}
}
$obj = new MainClass;
echo $obj->show();
echo $obj->test();
?>
Output:
Conclusion
This article, it is explained about PHP constants and magic constants with examples. These examples help to create their own constants and use them in the script with the help of the given syntax. This article also explains the rules on how to create PHP Constants and then how to use them within the script with different methods.
Recommended Articles
This is a guide to PHP Constants. Here we discuss the introduction, syntax, and examples to create constants in PHP along with magic constants. You may also look at the following articles to learn more –