Call a function

Lets just say that you are writing a template program and you want to add into a array of function calls, which you can add to as well on-the-fly. Within PHP you can use a function called call_user_func which allows you to call a function using a stringed parameter in the first parameter and the next parameter(s) (if you wish to pass more than one parameter) is next. Hopefully the below will demonstrate better.

<?php
  function test($var)
  {
    echo "test : " . $var;
  }
 
  function example($var)
  {
    echo "example : " . $var;
  }
 
  $funarr = array(array("test", "codingfriends"), array("example", "was here"));
 
  foreach ($funarr as $funarr_call)
  {
    call_user_func($funarr_call[0], $funarr_call[1]);
    echo "<br/>";
  }
?>

and the output would be

test : codingfriends
example : was here

Leave a Reply

Your email address will not be published. Required fields are marked *