Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

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

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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 GeeksforGeeks().




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

Output:

GeeksforGeeks

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


My Personal Notes arrow_drop_up
Last Updated : 25 Sep, 2019
Like Article
Save Article
Similar Reads
Related Tutorials