Learn PHP: Callbacks

Learn PHP: Callbacks
Want more? Explore the library at https://www.codecourse.com/lessons

Official site


RRelated Posts

Object Oriented PHP

Get the Code Here : http://goo.gl/Go6TM1 Best Book on PHP : http://goo.gl/yWMRqg Support me on Patreon : https://www.patreon.com/derekbanas?ty=h Previously I covered everything about

PHP Closures

PHP Functions PHP Functions allow you to combine several statements of code into a 'function'. These functions can then be called by their given name, and all of the code contained will run and


  • science blossom
    I wish you explained the benefit of it as well, personally, I think the second one might be a bit more efficient since it doesn't return a whole array and just picks the one index specified by the user and returns it.
  • Gabriel R.
    This is one of those concepts that even after you get it, you dont get it... mind boggling.
    its like a loop from function declaration to function calling. But for those watching, something that might help you understand the concept is remembering that functions can be refere to through variables.
  • Thomas Williams
    I can understand the use of callbacks in jQuery because it is asynchronus, but I don't see the point of a call back in php, apart from the fact you can call a function from a callback. I know this is just to show a callback working, but I would like to see some real world uses of a callback in php
  • Hai Wang
    It still not too late to chill in this awesome channel ,thanks so much.
  • rey mago
    I took a moment to look at the final code, and I understood the whole thing, thank you
  • Nilesh
    i got this but why $result is there in call_user_func() as parameter ??
  • Maz
    This video helped me to understand better anonymous functions, thank you again Alex!
  • vkubre
    Great one
  • Paulo Sandsten
    Which one is the callback function in CS lingo?

    The first one or the one in the string(); ?
  • Graham
    Good job thank you.
  • Paul Anekwe
    I pray they remove this callback, have struggled to find out why the feature.
  • Luke Vincent
    Hopefully I can describe what's happening in a way that's not too confusing:

    The function string() is passed both a string; 'Alex' and an anonymous function that doesn't yet do anything.
    call_user_func calls its first argument, which is the passed anonymous function, and passes its second argument to that function
    $name now essentially points to $results and is given the key upper in line 15 which returns the $string value modified by strtoupper.

    Here the callback is the anonymous function 1 (can be any function though) that is called by another function 2 ( string(callback) ) taking it as a parameter/argument.
    The point is that it allows events to unfold in a specific way.

    Consider this: You give your girlfriend a box (the parameter) with the instruction to open the box (the function 2) and within this box is the instruction to leave immediately (the function 1). The power is really in the callback being treated as an object, so you can change what's in the box dependant on the situation. If your girlfriend looks a bit moody have the instruction be to sit down and relax.

    Further reading: http://stackoverflow.com/questions/9596276/how-to-explain-callbacks-in-plain-english-how-are-they-different-from-calling-o
  • V. Peters
    I still don't get what the purpose is of callbacks, why should you do this like shown?
  • Ovidiu Drobotă
    I'm really lost here but I'll try to understand what's going one. I won't leave this video until I know what is happening. Guys, if you want to progress in PHP, just RELOAD the videos for many times. FOCUS and focus. Over time, you will understand. Trust me!
  • slidenerd
    callbacks are a real pain for a beginnner to understand whether its PHP, JS or even Java8 's new Lambda expressions which pretty much look like the same thing...
  • Lucian Ene
    very good lesson, helped me out to understand callback functionality
  • Tijn T

    function string($string, $callback) {
        $results = array(
            'upper' => strtoupper($string),
            'lower' => strtolower($string),
        if(is_callable($callback)) {
            call_user_func($callback, $results);

    string('Alex', function($name) {
        echo $name['lower'];
  • Peristilo peris
    I got totally lost here. I love Alex's videos, but this one is too obscure.
  • Oldeuboi
    I've been able to follow everything up to this point. :D Can't really wrap my head around using anonymous functions for callbacks. The number of variables used in this examples is also confusing me... :(
  • MrKrusas
    Maybe interesting, but this guy lacks the gift of teaching.
  • Glenn Sumner
    to understand what this does: it creates an ANONYMOUS USER FUNCTION which IF ITS CALLABLE calls the callback given by the first parameter, that can then be referenced using call_user_func()
    For more info check: http://php.net/manual/en/function.call-user-func.php and: http://php.net/manual/en/function.is-callable.php and:php.net/manual/en/functions.anonymous.php

    a simple example is: #2 (http://php.net/manual/en/function.call-user-func.php?#example-5185)
    function barber($type)
        echo "You wanted a $type haircut, no problem\n";
    call_user_func('barber', "mushroom");
    call_user_func('barber', "shave");

    You wanted a mushroom haircut, no problem
    You wanted a shave haircut, no problem
  • dirtyweasel
    Basically it allows you to modularize your code and use it in response to specific condiions. He wrotethe whole function in the callback but he could have just used the function name. Had he put in an if loop to check for the existence of the uppercase entry in the array they he could have used the function with any array without binding it to a specific object. This makes the function usable across a whole application.
  • javier
    great video, thanks.
  • Miroslav Trninić
    How to test php callbacks ?
  • Stefany Dyulgerova
    I believe these callback stuff are only used in functional programming. i.e. the newest trend in programming. It used to be OOP, now it is functional.
  • Alexandre Janini
    I agree with the other commenters. I cannot see how this would be useful... Maybe I'm just not there yet.
  • Rolling Houses UK
    I've never been able to find anything, either in the php docs or anywhere else online (maybe I just haven't searched hard enough though) that explains in a pragmatic way what a callback is actually for in a realworld, non-abstract way.
  • Michael Stokes
    It would be cool if you made a video showing a more complex and real world example of where this would be most useful.
  • Tony Radford
    Hi Alex, not sure why or how you would use this in the real world, and what the benefits of the extra coding would be?  Thanks for all the hard work you put into this series.
  • perkin524
    Thanks for being so generous as provide this wealth of tutorials - great stuff. I have followed this series perfectly up to here but in this video you completely lost me. I just can't understand what you're doing. Can we at some point have a further video on callbacks?