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.
In PHP, a function is declared with the function keyword prefixed with the function name and the calling of a function in a program is done by just calling the name of the function wherever required.
There are two variables you can use while writing command line applications with PHP: $argc and $argv. The first is the number of arguments plus one (the name of the script running). The second is an array containing the arguments, starting with the script name as number zero ($argv[0]).
So as long as you have your function defined when you hit a certain spot in your code it works fine, no matter if it's called from within another function. Show activity on this post. If you define function within another function, it can be accessed directly, but after calling parent function.
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.
Use
php -r 'include "/var/www/test/address.php";exportAddress(1);'
where "/var/www/test/arr.php"
is the file name, including path, and exportAddress()
is a function inside that file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With