Last active
August 29, 2015 13:57
-
-
Save miguelsaddress/9483837 to your computer and use it in GitHub Desktop.
How to use a callback in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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