Anonymous function example in PHP

 

  1. <?php
  2.  
  3. /**
  4. * An array function to work with its individual value whatever we want.
  5. * @param array $array
  6. * @param function $function
  7. */
  8. function anonymous_test($array, $function){
  9. foreach ($array as $single){
  10. $function($single);
  11. }
  12. }
  13. ?>

 

 

  1. <?php
  2. /**
  3. * Here we are writing a function which will print only if it's printable.
  4. */
  5. $arr = [1,2,3,['A','B'],4,5];
  6. anonymous_test($arr, function($value){
  7. if(!is_array($value) && !is_object($value)){
  8. echo $value;
  9. }
  10. });