Call a function dynamically in PHP

Description

The following code shows how to call a function dynamically.

Example


<?php//from w  w w.j a  v  a  2  s .c  om
  function write($text)
  {
    print($text);
  }

  function writeBold($text)
  {
    print("<b>$text</b>");
  }

  $myFunction = "write";
  $myFunction("Hello!");
  print("<br>\n");

  $myFunction = "writeBold";
  $myFunction("Goodbye!");
  print("<br>\n");
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Language Basic »




PHP Introduction
PHP Operators
PHP Statements
Variable
PHP Function Create
Exception
PHP Class Definition