How to call a function by its name as a string in PHP?

In PHP, how to call a function by its name as a string? The name strings may be known only at run time.

If the string containing the function name is $func, you may call the function like

$func()

or

call_user_func($func)

using call_user_func().

The call_user_func() function supports more usage patterns, like

call_user_func( array($obj,$func), $params )

to call object $obj‘s function named $func with parameters $params.

Leave a Reply

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