Dynamic Function Calls PHP

You might find yourself in the position of not knowing which function should be called when you are writing a script. You want to decide based on data you have during execution. One way to accomplish this is to set a variable with the name of a function and then use the variable as if it were a function.

If you follow a variable with parentheses, the value of the variable will betreated as the name of a function. demonstrates this. Keep in mind that you can't refer to built-in functions in this way. Setting myFunction to be print will cause an error.

Dynamically Calling a Function

<?
function write($text)
{
print($text);
}
function writeBold($text)
{
print("<B>$text</B>");
}
$myFunction = "write";
$myFunction("Hello!");
print("<BR> ");
$myFunction = "writeBold";
$myFunction("Goodbye!");
print("<BR> ");
?>


Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PHP Topics