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

how to use git restore and completely ignore the changes: 3 scenarios (video)
Nov 27, 2022 programming

how to use git restore and completely ignore the changes: 3 scenarios (video)

we are going to talk about ways to ignore the edits we made in a git project and restore the changes by git restore ...

9 Min Read Read More
PHP Read and Write to Files
Mar 21, 2023 programming

PHP Read and Write to Files

Today we are going to learn about working with files in PHP.There are many functions that can help you read the contents of a file. we are going through the most common functions. ...

11 Min Read Read More
PHP Static
Mar 22, 2023 programming

PHP Static

Today we are going to talk about Static methods and properties in PHP. You can access the methods and properties of a class without create a new object but for that you need to know about static. ...

5 Min Read Read More
PHP Pass Arguments by Reference
Jan 12, 2023 programming

PHP Pass Arguments by Reference

Today we are going to talk about splat passing arguments by reference in PHP. By default when we pass argument to a function we pass it by value. ...

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