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

PHP Loops
Mar 21, 2023 programming

PHP Loops

Today we are going to learn about loops in PHP. Loop is another important concept in programming and we are going to learn everything about loops in PHP. ...

11 Min Read Read More
PHP Array Merge
Feb 14, 2023 programming

PHP Array Merge

Today we are going to talk about array merge function in PHP. This function is very useful and merges all the arrays that you pass as arguments and returns an array. ...

3 Min Read Read More
PHP Array Diff
Feb 14, 2023 programming

PHP Array Diff

Today we are going to talk about array diff functions in PHP. If you want to find the items in your array that are not present in the other arrays, array diff is your friend. ...

5 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

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 ...