Call a PHP function from the command line

Jack Smit picture Jack Smit · Dec 7, 2012 · Viewed 54.6k times · Source

I have a file called address.php with a few functions in it. I want to call a specific function in that file from the command line. How?

The name of the function is called exportAddress and that function expects a single parameter.

Answer

Tim S. picture Tim S. · Dec 7, 2012

By using the -r parameter you can run a script in-line.

php -r "require 'address.php'; exportAddress(12345);"

There are no other options. A function in PHP can only be called by a PHP script.