How to get a Function Name Inside a Function In PHP

admin_img Posted By Bajarangi soft , Posted On 03-12-2020

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.

function name inside a function

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

Example 1:

<?php 

function companywebsite() { 
    
    // Magic function constant which 
    // holds the function name, or 
    // {closure} for anonymous functions 
    $string = __FUNCTION__; 
    
    // Display the result 
    echo $string; 


// Function call 
companywebsite(); 

?>

 

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

<?php 

function company() { 
    
    // Magic function constant which 
    // holds the class method name 
    $string = __METHOD__; 
    
    // Display the result 
    echo $string; 


// Function call 
company(); 

?> 

Related Post