Current Location: Home> Latest Articles> How to Use get_defined_functions to List All PHP Built-in and User-defined Functions?

How to Use get_defined_functions to List All PHP Built-in and User-defined Functions?

gitbox 2025-08-22

How to Use get_defined_functions to List All PHP Built-in and User-defined Functions?

In PHP, built-in functions are predefined functions that can be used directly without writing any code. User-defined functions, on the other hand, are created by developers based on their needs. If you want to list all built-in and user-defined functions in PHP, you can use the get_defined_functions function. This function not only lists all defined functions in the current PHP environment but also categorizes them into built-in and user-defined functions.

1. Introduction to get_defined_functions

get_defined_functions is a built-in PHP function that returns an array containing all defined functions. This array has two keys:

  • internal: an array containing all built-in functions.

  • user: an array containing all user-defined functions.

This function does not take any parameters. When called, it returns a multidimensional array, making it easy for developers to review all defined functions.

2. How to Use get_defined_functions to List Functions

You can directly use get_defined_functions to get a list of all functions. Below is an example code showing how to list built-in and user-defined functions.

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// Define a simple custom function</span></span><span>
</span><span><span class="hljs-function"><span class="hljs-keyword">function</span></span></span><span> </span><span><span class="hljs-title">my_custom_function</span></span><span>(</span><span><span class="hljs-params"></span></span><span>) {
    </span><span><span class="hljs-keyword">return</span></span><span> </span><span><span class="hljs-string">"Hello, World!"</span></span><span>;
}
<p></span>// Get all defined functions<br>
$defined_functions = get_defined_functions();</p>
<p>// List built-in functions<br>
echo "Built-in functions:\n";<br>
print_r($defined_functions['internal']);</p>
<p>// List user-defined functions<br>
echo "\nUser-defined functions:\n";<br>
print_r($defined_functions['user']);<br>
?><br>
</span>

In this example, we first defined a simple function my_custom_function, then called get_defined_functions to get all functions in the current environment. By printing the array, we can clearly see the difference between built-in functions and user-defined functions.

3. Output Explanation

When you run the above code, the output looks something like this:

<span><span>Built-in functions:
Array
(
    [</span><span><span class="hljs-meta">0</span></span><span>] =&gt; abs
    [</span><span><span class="hljs-meta">1</span></span><span>] =&gt; acos
    [</span><span><span class="hljs-meta">2</span></span><span>] =&gt; acosh
    ...
)
<p>User-defined functions:<br>
Array<br>
(<br>
[</span>0] => my_custom_function<br>
)<br>
</span>

As shown above, get_defined_functions lists all built-in functions in the internal array, while our custom function my_custom_function is listed in the user array.

4. Notes

  • get_defined_functions only lists functions defined in the current script, including functions in the current scope. For functions imported from external files (e.g., through include or require), if those files are loaded before execution, the functions will also be listed.

  • If you have a large number of user-defined functions, get_defined_functions can be very useful, especially in debugging and documentation generation scenarios.

5. Conclusion

get_defined_functions is a very practical function that helps developers quickly list all defined functions in a PHP environment. With it, we can easily distinguish between built-in and user-defined functions, making PHP code management and debugging more efficient.