Home > Article > Backend Development > PHP Magic Constants
In PHP Magic Constants, there are in total of eight constants that change their dependency based on where they are used. All these magical constants are resolved at compilation time and not like the constants that we use on a regular basis which we generally resolve at run time. These magical constants are case-insensitive. These constants are predefined constants and start with a double underscore (__) and also ends with a double underscore. These constants are the most practical and most useful constants in PHP. They are simple variables but have a predefined meaning to them. These constants are used to print the user-defined inputs and process the output to display on the screen.
ADVERTISEMENT Popular Course in this category MAGIC BULLET LOOKS - Specialization | 2 Course SeriesStart Your Free Software Development Course
Web development, programming languages, Software testing & others
There is a total of eight magic constants in PHP mentioned below:
Below are the examples of How Magic Constants work in PHP:
In PHP, we can use magic constants in very easy code, too difficult ones that we use on our daily basis. Let’s take an example to see how it works:
Code:
<!DOCTYPE html> <html> <body> <?php echo "<h1>Example for __LINE__ constant</h1>"; echo "The line number is " . __LINE__ . "<br><br>";// prints the current line number i.e;7 ?> </body> </html>
Output:
Code:
<!DOCTYPE html> <html> <body> <?php echo "<h2>Example for __FILE__ constant</h2>"; echo __FILE__ . "<br><br>";//prints the full path of the file with extension ?> </body> </html>
Output:
Code:
<!DOCTYPE html> <html> <body> <?php echo "<h3>Example for __DIR__ constant</h3>"; echo __DIR__ . "<br><br>";//prints the full path of the directory where the script is placed. ?> </body> </html>
Output:
Code:
<!DOCTYPE html> <html> <body> <?php function amount() { echo 'the function name is '. __FUNCTION__ . "<br><br>";//the function name is amount. } amount(); ?> </body> </html>
Output:
Code:
<!DOCTYPE html> <html> <body> <?php //Using magic constant inside function. function amount() { echo 'the function name is '. __FUNCTION__ . "<br><br>";//the function name is amount. } amount(); echo 'the function name is '. __FUNCTION__ ."<br><br>"; ?> </body> </html>
Output:
Code:
<!DOCTYPE html> <html> <body> <?php echo "<h2>Example for __CLASS__</h2>"; class xyz { public function __construct() { ; } function xyz_method() { echo __CLASS__ . "<br>";//prints the name of the class xyz mentioned above. } } $a = new xyz; $a->xyz_method(); ?> </body> </html>
Output:
Code:
<!DOCTYPE html> <html> <body> <?php class abc { function test_abc() { echo __CLASS__;//will always print parent class which is abc mentioned above. } } class xyz extends abc { public function __vowels() { ; } } $b = new xyz; $b->test_abc(); ?> </body> </html>
Output:
Code:
<!DOCTYPE html> <html> <body> <?php echo "<h4>Example for __TRAIT__</h4>"; trait create_trait { function trait() { echo __TRAIT__;//will print name of the trait create_trait mentioned above. } } class new_class { use create_trait; } $c = new new_class; $c-> trait (); ?> </body> </html>
Output:
Code:
<!DOCTYPE html> <html> <body> <?php echo "<h2>Example for __METHOD__</h2>"; class method { public function __parameter() { echo __METHOD__ . "<br><br>";//print method::__parameter } public function method_fun(){ echo __METHOD__;//print meth::method_fun } } $z = new method; $z->method_fun(); ?> </body> </html>
Output:
The output of the respective functions is mentioned above. The line constant will print the current line of the file leela.php stored in the localhost. The file constant will print the file name along with the path, as shown in the output. The dir constant or dirname will print the current one’s directory path or the mentioned one: the method and class constant prints the method name and class name mentioned in the code. If the constants are mentioned outside of method and class, then it will not print anything on the screen as it is out of the scope, and similarly, the other constant’s output is mentioned above.
In this article, we learned all the magic constants of PHP and its usage. It can be used in small and tiny programs to big or large programs. Developers can use these constants for backtracking any issue as to where the error might have occurred. These constants will help the developers or users to check on the code as to where they are currently present.
The above is the detailed content of PHP Magic Constants. For more information, please follow other related articles on the PHP Chinese website!