Home >Backend Development >PHP Tutorial >How to Pass Functions as Arguments in PHP?
In PHP, you can extend the functionality of your code by passing functions as arguments to other functions. This technique, known as higher-order programming, allows you to create flexible and reusable code.
Syntax in PHP >= 5.3.0
To pass a function as an argument, you can use anonymous functions, also known as lambdas. The syntax for an anonymous function in PHP is:
<code class="php">function ($args) { // Function body }</code>
For example, in JavaScript, you would write:
<code class="javascript">object.exampleMethod(function(){ // some stuff to execute });</code>
In PHP, you would pass the anonymous function as an argument to the exampleMethod function:
<code class="php">function exampleMethod($anonFunc) { // Execute the anonymous function $anonFunc(); } exampleMethod(function(){ // Some stuff to execute });</code>
Note: Anonymous functions are available in PHP versions 5.3.0 and later.
The above is the detailed content of How to Pass Functions as Arguments in PHP?. For more information, please follow other related articles on the PHP Chinese website!