Free cookie consent management tool by TermsFeed Generator PHP Callback Functions | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP Callback Functions

PHP Callback Functions

Last Updated on Jan 08, 2023

Introduction

We have already mentioned callback functions when we were talking about array map. but now let's talk more in details about callback functions, what they are and how we have callback function in our own functions.

Callback Function as Argument

Let's say we want to define a function and this function get's 2 arguments. the first one is a value and the second one is a function that changes the value of the first argument. something like this:

function changeIt($a,$callback){
}

now all I have to do to call the $callback argument like a function is to add () after it's name. it would cause it to be called like a function. then inside () I can have arguments and since the function is going to change the value of the first argument I can pass $a to it. so the final function would be like this:

function changeIt($a,$callback){
    return $callback($a);
}

that's it. that's all we have to do.

Predefined Function

Now let's define another function call add that gets a number as an argument and adds 2 to that number and return it.

function addTwo(int $number){
    return $number + 2;
}

now we can call our own change function with a number as the first argument and the addTwo function as the second argument and run it.

Right now our addTwo function is considered as a predefined function. it means it has a name and it has been defined prior to using it in our function call.so all we have to do is to write its name as a string and PHP will take care of the rest.

function changeIt($a,$callback){
    $callback($a);
}

function addTwo(int $number){
    return $number + 2;
}

echo changeIt(12,"addTwo");
// 14

Anonymous Function

Now let's say we don't want to define our function and we want to create the function right where we write the argument. in that case we can use anonymous functions. all we have to do is to define them where we wrote the name of the function before. like this:

echo changeIt(12,function (int $number){
    return $number + 2;
});
// 14

That's it. that's how simple it is. having callback functions in our functions is very simple yet it's very powerful. a lot of builtin functions in PHP are using callback functions as well.

Conclusion

Now you know how to work with callback functions in PHP.

I recommend you to open a PHP files and try to work with both predefined and anonymous functions.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • Callback Functions
  • Anonymous Function
  • Predefined functions

Category: programming

Tags: #php

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

the ultimate guide to Git: learn everything you need in less than 1 hour
Nov 28, 2022 programming

the ultimate guide to Git: learn everything you need in less than 1 hour

Here is everything you need to know about git in a series of short question and answers. ...

11 Min Read Read More
PHP Arrays
Mar 21, 2023 programming

PHP Arrays

Today we are going to learn about arrays in PHP. Array is a datatype that can hold more than one value so instead of having multiple variables you can hold all those values in one variable. ...

9 Min Read Read More
PHP Testing with PHPUnit and Pest
Mar 22, 2023 programming

PHP Testing with PHPUnit and Pest

Today we are going to write tests with PHPUnit and Pest in PHP. Testing is a very important step in developing an application. phpunit and pest make it really easy to write tests in php ...

12 Min Read Read More
AngularJS Beginner to Advance 2025: The Complete Guide for Modern Web Developers
Jul 18, 2025 programming

AngularJS Beginner to Advance 2025: The Complete Guide for Modern Web Developers

In today’s web development landscape, AngularJS may no longer dominate headlines, but it still powers countless legacy applications. Many organizations—especially in finance, healthcare, and enterprise software—rely on AngularJS systems that require ongoing maintenance and enhancements. ...

43 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...