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.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
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.
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; ?>
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 $.
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.
Code:
<?php // program to demonstrate in PHP 7 using const keyword const TEXT = 'PHP PROGRAMMING!'; echo TEXT; echo constant("TEXT"); ?>
Output:
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 :
The following are the rules to define PHP constants.
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 ?>
It starts with a double underscore
This gives the current line number.
Code:
<?php //example to demonstrate PHP magic constant __LINE__ echo 'I am at Line number '. __LINE__; ?>
Output:
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:
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:
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:
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.
The above is the detailed content of PHP Constants. For more information, please follow other related articles on the PHP Chinese website!