Home > Article > Backend Development > What are magic constants in PHP
Magic constants are predefined constants in PHP that can change depending on where they are used. This article will introduce you to PHP magic constants and introduce some commonly used PHP magic constants. I hope it will be helpful to you.
Magic constants start with two underscores (__) and end with two underscores (__). Below we will introduce some commonly used PHP magic constants.
__LINE__
__LINE__ constant returns the current line number of the file. Example:
"; // 第3行 echo "当前行数:" . __LINE__ . "
"; // 第4行 echo "当前行数:" . __LINE__ . "
"; // 第5行 ?>
Output:
__FILE__
__FILE__ constant return is The full path and name of the PHP file being executed; if used within an include, returns the name of the included file.
Output:
__DIR__
__DIR__ constant returns the directory of the file . If used within an include, returns the directory containing the file. Example:
Output:
##__FUNCTION__
## The #__FUNCTION__ constant returns the name of the current function. Example:
Output:
__CLASS__ Constant returns the name of the current class.
getClassName(); ?>Output:
__METHOD__ constant return The name of the current class method.
myMethod(); ?>Output:
__NAMESPACE__constant return The name of the current namespace.
getNamespace(); // Displays: MyNamespace ?>Output:
The above is the entire content of this article, I hope it will be helpful to everyone's learning. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !
The above is the detailed content of What are magic constants in PHP. For more information, please follow other related articles on the PHP Chinese website!