English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn how to use constants to store fixed values in PHP.
Constants are names or identifiers of fixed values. Constants are similar to variables, except that once a constant is defined, it cannot be undefined or changed (Magic constantsexcept).
Constants are very useful for storing data that will not change during the execution of the script. Common examples of such data include configuration settings, such as database usernames and passwords, the basic URL of the website, company name, etc.
Constants are defined using PHP's define() function, which accepts two parameters: the name of the constant and its value. Once the constant value is defined, it can be accessed at any time by referencing its name. Here is a simple example:
<?php // Define constants define("SITE_URL", "https://www.oldtoolbag.com/"); // Using constants echo 'Thank you for visiting - ' . SITE_URL; ?>Test and see‹/›
The output of the above code will be:
Thank you for visiting-https://www.oldtoolbag.com/
The PHP echo statement is usually used to display data or output data to the web browser. We will learn about this statement in detail in the next chapter.
Tip:By storing values in constants instead of variables, it can ensure that the value will not be unexpectedly changed during the execution of the application.
A dollar sign ($) does not need to be written before constants, but it must be written before the name of a variable.
Constants cannot be defined by simple assignment; they must be defined using the define() function.
Constants can be defined and accessed at any location without considering variable scope rules.
Once a constant is set, it cannot be redefined or undefined.
// Valid constant names define("ONE", "first thing"); define("TWO2", "second thing"); define("THREE_3", "third thing"); define("__THREE__", "third value"); // Invalid constant name define("2TWO", "second thing");
PHP provides a large number of predefined constants for any script it runs.
There are five magic constants that change according to the position used. For example, the value of __LINE__ depends on the line used in the script. These special constants are not case-sensitive, as shown below-
Below are some 'magic' PHP constants-
Sr.No | Name & Description |
---|---|
1 | __LINE__ The current line number of the file. |
2 | __FILE__ The full path and file name of the file. If used in Include, it returns the name of the included file. From PHP 4.0.2starting, __file__ always contains the absolute path, while in older versions, it may contain a relative path in some cases. |
3 | __FUNCTION__ function name. (In PHP 4.3.0 adds) from PHP 5starting, this constant returns the name of the declared function (case-sensitive). In PHP4inside, its value is always lowercase. |
4 | __CLASS__ class name. (In PHP 4.3.0 adds) from PHP 5Starting from, this constant returns the name of the declared class (case-sensitive). In PHP4inside, its value is always lowercase. |
5 | __METHOD__ Class method names. (In PHP5method name as declared (case-sensitive). |
Constant names must follow the sameVariable namesThe same rule applies, which means that valid constant names must start with a letter or underscore, followed by any number of letters, numbers, or underscores, but there is an exception:Constant names do not require a prefix of $
Note:By convention, constant names are usually written in uppercase. This is because they are easy to identify and distinguish from variables in the source code.