PHP Constants

PHPz
Release: 2024-08-29 13:01:23
Original
1342 people have browsed it

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 Tests

Start 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.

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);
Copy after login

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!');
Copy after login

A constant can also be defined using const construct.

<?php
const MSG = "WELCOME";
echo MSG;
?>
Copy after login

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;
?>
Copy after login

Output:

PHP Constants

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");
?>
Copy after login

Output:

PHP Constants

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();
?>
Copy after login

Output :

PHP Constants

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
?>
Copy after login

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__;
?>
Copy after login

Output:

PHP Constants

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__;
?>
Copy after login

Output:

PHP Constants

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();
?>
Copy after login

Output:

PHP Constants

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();
?>
Copy after login

Output:

PHP Constants

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.

The above is the detailed content of PHP Constants. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template