Callback Functions – PHP

Callback Functions

A callback function, which is often just called \”callback,\” is a function that is passed to another function as an argument.

A callback function can be any function that is already in use. To use a function as a callback function, you must pass the function\’s name as an argument to another function:

Example

Give PHP\’s array map() function a callback to figure out how long each string in an array is:

<!DOCTYPE html>
<html>
<body>

<?php
function my_callback($item) {
return strlen($item);
}

$colors = [\”Red\”, \”Green\”, \”Blue\”, \”Black\”];
$lengths = array_map(\”my_callback\”, $colors);
print_r($lengths);
?>

</body>
</html>

Output

Array ( [0] => 3 [1] => 5 [2] => 4 [3] => 5 )

Example 2

As of PHP version 7, anonymous functions can be passed as callback functions:

<!DOCTYPE html>
<html>
<body>

<?php
$colors = [\”Red\”, \”Orange\”, \”Yellow\”, \”Pink\”];
$lengths = array_map( function($item) { return strlen($item); } , $colors);
print_r($lengths);
?>

</body>
</html>

Output

Array ( [0] => 3 [1] => 6 [2] => 6 [3] => 4 )

 

User Defined Functions callbacks.

Callback functions can also be passed as arguments to user-defined functions and methods. To use callback functions inside a user-defined function or method, add parentheses around the variable and pass arguments as you would with regular functions:

Example

Use a user-defined function to run a callback:

<!DOCTYPE html>
<html>
<body>

<?php
function mark($var) {
return $var . \”! \”;
}

function ask($var) {
return $var . \”? \”;
}

function wordmark($var, $mark) {
// Calling the $mark callback function
echo $mark($var);
}

// Pass \”mark\” and \”ask\” as callback functions to wordmark()
wordmark(\”Loream ipsum\”, \”mark\”);
wordmark(\”Loream ipsum\”, \”ask\”);
?>

</body>
</html>

Output

\"Callbacks

Related Article
50+ PHP Interview Questions and Answers 2023

1. Differentiate between static and dynamic websites. Static Website The content cannot be modified after the script is executed The Read more

All We Need to Know About PHP Ecommerce Development

  Many e-commerce sites let you search for products, show them off, and sell them online. The flood of money Read more

PHP Custom Web Development: How It Can Be Used, What Its Pros and Cons Are,

PHP is a scripting language that runs on the server. It uses server resources to process outputs. It is a Read more

PHP Tutorial

PHP Tutorial – Complete Guide for Beginners to Advanced Welcome to the most comprehensive PHP tutorial available online at PHPOnline.in Read more

Introduction of PHP

Introduction to PHP – Learn PHP from Scratch with Practical Examples Welcome to your complete beginner's guide to PHP. Whether Read more

Syntax Overview of PHP

Syntax Overview of PHP (2025 Edition) Welcome to phponline.in, your one-stop platform for mastering PHP. This comprehensive, SEO-rich tutorial on Read more

Environment Setup in PHP

Setting Up PHP Environment (Beginner’s Guide) If you’re planning to learn PHP or start developing websites using PHP, the first Read more

Variable Types in PHP

PHP Variable Types: Complete Beginner's Guide to PHP Data Types Welcome to phponline.in, your trusted source for beginner-to-advanced level PHP Read more

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Prove your humanity: 10   +   8   =