Thursday, September 4, 2025
HomeLanguagesHow to get the function name inside a function in PHP ?

How to get the function name inside a function in PHP ?

To get the function name inside the PHP function we need to use Magic constants(__FUNCTION__).

Magic constants: Magic constants are the predefined constants in PHP which is used on the basis of their use. These constants are starts and end with a double underscore (__). These constants are created by various extensions.

Syntax:

$string = __FUNCTION__

Description: This constant is used to return the function name, or {closure} for anonymous functions.

Program 1: PHP program to print function name inside the function neveropen().




<?php
  
function neveropen() {
      
    // Magic function constant which
    // holds the function name, or 
    // {closure} for anonymous functions
    $string = __FUNCTION__;
      
    // Display the result
    echo $string;
}
  
// Function call
neveropen();
  
?>


Output:

neveropen

Program 2: PHP program to print the function name inside the function Geeks().




<?php
  
function Geeks() {
      
    // Magic function constant which
    // holds the class method name
    $string = __METHOD__;
      
    // Display the result
    echo $string;
}
  
// Function call
Geeks();
  
?>


Output:

Geeks

Reference: https://www.php.net/manual/en/language.constants.predefined.php

RELATED ARTICLES

Most Popular

Dominic
32260 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6625 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6694 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS