How does a PHP function return the function name?

WBOY
Release: 2024-04-11 08:39:02
Original
411 people have browsed it

Get the name of the executing function through the FUNCTION magic constant in PHP: Syntax: $function_name = __FUNCTION__; Usage: Use FUNCTION inside the function to get the name of the calling function; Note: It can only be used inside the function, and only the function name is returned. , does not contain a namespace or class name, and returns an empty string when used as an anonymous function.

PHP 函数如何返回函数名?

#How does a PHP function return the function name?

A powerful feature in PHP is the ability to get the name of the function being executed via the __FUNCTION__ magic constant. This constant can be used inside a function to get the name of the calling function.

Syntax:

$function_name = __FUNCTION__;
Copy after login

Practical case:

The following is an example of getting the function name inside the function:

function get_function_name() {
  echo "当前函数名:" . __FUNCTION__;
}

get_function_name();
Copy after login

This code will output:

当前函数名:get_function_name
Copy after login

Other notes:

  • __FUNCTION__ Constants can only be used inside functions .
  • This constant returns only the name of the function, not the namespace or class name.
  • If the function has no name (such as an anonymous function), __FUNCTION__ will return an empty string.

The above is the detailed content of How does a PHP function return the function name?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!