Skip to content

Instantly share code, notes, and snippets.

@miguelsaddress
Last active August 29, 2015 13:57
Show Gist options
  • Save miguelsaddress/9483837 to your computer and use it in GitHub Desktop.
Save miguelsaddress/9483837 to your computer and use it in GitHub Desktop.
How to use a callback in PHP
function aCallback($param1, $param2){
print "I am a callback";
print "\n";
print_r( $param1 );
print "\n";
print_r( $param2 );
print "\n";
print "End of the calback";
print "\n";
}
function myFunction( $callback, $p1, $p2="default value" ){
print "doing things inside the function";
print "\n";
$callback($p1, $p2);
print "you can do things here as well";
print "\n";
}
$p1 = array("one", "two", "three");
$p2 = "Caramba!";
myFunction( 'aCallback', $p1);
myFunction( 'aCallback', $p1, $p2);
Miguel at 12:15 in path ~/Desktop
$ php test.php
doing things inside the function
I am a calback
Array
(
[0] => one
[1] => two
[2] => three
)
default value
End of the calback
you can do things here as well
doing things inside the function
I am a calback
Array
(
[0] => one
[1] => two
[2] => three
)
Caramba!
End of the calback
you can do things here as well
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment