Reputation: 4102
I am studying PHP and i am trying to understand about callback functions, I really looked around at the manual, At stackoverflow and more websites, And i really dont understand what is PHP CallBack Function , If can some one can please help me understand about this functions, I am looking for simple explanation/guide thank you all and have a nice day.
Upvotes: 2
Views: 1863
Reputation: 10074
Take a look at Wikipedia - Callback
In computer programming, a callback is a reference to a piece of executable code that is passed as an argument to other code. This allows a lower-level software layer to call a subroutine (or function) defined in a higher-level layer.
It's a function what you pass to your method or some other function, so it can be called later during that method - function execution.
For example you have callback beforeSave
and you want do some logic before saving data to database file etc.. (in one place - DRY). You add logic into beforeSave
callback and this callback being called before data saved.
Same is with function on manual for example array_filter($input, callback)
it requires that you pass some function to be executed with $input data.
For ex. pass anonymous function:
array_filter($input, function($var) {
// returns whether the input integer is odd
return($var & 1)
});
Will return to you all odd array values, you can change logic in anonymous function to what you want, but array_filter
internal mechanic will be always the same (iterator algo)
Upvotes: 5