Home  >  Article  >  Backend Development  >  What are magic constants in PHP

What are magic constants in PHP

青灯夜游
青灯夜游Original
2019-01-25 17:38:353753browse

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.

What are magic constants in PHP

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:

What are magic constants in PHP

__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:

What are magic constants in PHP

__DIR__

__DIR__ constant returns the directory of the file . If used within an include, returns the directory containing the file. Example:

Output:

What are magic constants in PHP

##__FUNCTION__
## The #__FUNCTION__ constant returns the name of the current function. Example:

Output:


What are magic constants in PHP

##__CLASS__

__CLASS__ Constant returns the name of the current class.

getClassName(); 
?>

Output:


What are magic constants in PHP

__METHOD__

__METHOD__ constant return The name of the current class method.

myMethod(); 
?>

Output:


What are magic constants in PHP

__NAMESPACE__

__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! ! ! What are magic constants in PHP

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!

Statement:
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