2

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.

2
  • Do you have read this page? php.net/manual/en/language.types.callable.php Commented Jul 21, 2012 at 9:01
  • What is it exactly that you don't understand? What part is unclear? Commented Jul 21, 2012 at 9:04

1 Answer 1

5

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for you answer Aurimas Ličkus, have a nice day.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.