Loading

PHP

What is PHP Callback Functions?. The Complete PHP Developer Course 2023 [Videos].

A callback function (often referred to as just "callback") is a function which is passed as an argument into another function.

Callback Functions

A callback function (often referred to as just "callback") is a function which is passed as an argument into another function.

Any existing function can be used as a callback function. To use a function as a callback function, pass a string containing the name of the function as the argument of another function:

Example

Pass a callback to PHPs array_map() function to calculate the length of every string in an array:


function my_callback($item) {
  return strlen($item);
}

$strings = ["apple""orange""banana""coconut"];
$lengths = array_map("my_callback", $strings);
print_r($lengths);
?>

Starting with version 7, PHP can pass anonymous functions as callback functions:

Example

Use an anonymous function as a callback for PHPs array_map() function:


$strings = ["apple""orange""banana""coconut"];
$lengths = array_map( function($item) { return strlen($item); } , $strings);
print_r($lengths);
?>


Callbacks in User Defined Functions

User-defined functions and methods can also take callback functions as arguments. To use callback functions inside a user-defined function or method, call it by adding parentheses to the variable and pass arguments as with normal functions:

Example

Run a callback from a user-defined function:


function exclaim($str) {
  return $str . "! ";
}

function ask($str) {
  return $str . "? ";
}

function printFormatted($str, $format) {
  // Calling the $format callback function
  echo $format($str);
}

// Pass "exclaim" and "ask" as callback functions to printFormatted()
printFormatted("Hello world""exclaim");
printFormatted("Hello world""ask");
?>

See All

Comments (184 Comments)

Submit Your Comment

See All Posts

Related Posts

PHP / Youtube

How we can create website on wamp server 3.0?

The A in WAMP stands for Apache. Apache is server software that is used to serve webpages. Whenever someone types in your WordPress websites URL, Apache is the software that serves your WordPress site. The M in WAMP stands for MySQL. MySQL is a database management system.
27-dec-2020 /6 /184

PHP / Youtube

What is wamp server 3.0 ? What is Appache Server? How we can develop site in wamp server?

PHP is a server-side programming language. HTML is a client-side scripting language. PHP is used in backend development, which interacts with databases to retrieve, store, and modify the information. HTML is used in frontend development, which organizes the content of the website.
10-Feb-2021 /6 /184

PHP / Youtube

How we can develop basic site in php with html?

PHP is a server-side programming language. HTML is a client-side scripting language. PHP is used in backend development, which interacts with databases to retrieve, store, and modify the information. HTML is used in frontend development, which organizes the content of the website.
9-Feb-2021 /6 /184