从命令行调用php函数

时间:2021-08-11 22:29:46

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

我有一个名为address.php的文件,其中包含一些函数。我想从命令行调用该文件中的特定函数,怎么做?函数的名称称为exportAddress,该函数需要单个参数

4 个解决方案

#1


48  

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

通过使用-r参数,您可以在线运行脚本。

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

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

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

没有其他选择。 PHP中的函数只能由PHP脚本调用。

#2


3  

Add this to the top of the file "/var/www/test/address.php"...

将其添加到文件“/var/www/test/address.php”的顶部...

foreach ($argv as $i=>$arg )
{
    if ( $arg == "exportAddress" )
    { 
        exportAddress($argv[$i+1]);
    }
}

then from the command line execute#> php /var/www/test/address.php exportAddress 12345

然后从命令行执行#> php /var/www/test/address.php exportAddress 12345

#3


1  

php -r 'include "/var/www/test/address.php";exportAddress(1);'

php -r'include“/var/www/test/address.php";exportAddress(1);'

where "/var/www/test/arr.php" is file name including path and exportAddress() is function inside that file

其中“/var/www/test/arr.php”是包含路径的文件名,而exportAddress()是该文件中的函数

#4


1  

you can make your file "somefile.php" organized as follows:

你可以使你的文件“somefile.php”组织如下:

function func1(){....}
function func2(){....}
function func3(){....}
....
foreach ($argv AS $arg){
    function_exists($arg) AND call_user_func($arg);
}

Then from command line or Linux cronjob, you run the following command

然后从命令行或Linux cronjob运行以下命令

php /path/to/somefile.php arg1 arg2 arg3 ...

#1


48  

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

通过使用-r参数,您可以在线运行脚本。

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

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

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

没有其他选择。 PHP中的函数只能由PHP脚本调用。

#2


3  

Add this to the top of the file "/var/www/test/address.php"...

将其添加到文件“/var/www/test/address.php”的顶部...

foreach ($argv as $i=>$arg )
{
    if ( $arg == "exportAddress" )
    { 
        exportAddress($argv[$i+1]);
    }
}

then from the command line execute#> php /var/www/test/address.php exportAddress 12345

然后从命令行执行#> php /var/www/test/address.php exportAddress 12345

#3


1  

php -r 'include "/var/www/test/address.php";exportAddress(1);'

php -r'include“/var/www/test/address.php";exportAddress(1);'

where "/var/www/test/arr.php" is file name including path and exportAddress() is function inside that file

其中“/var/www/test/arr.php”是包含路径的文件名,而exportAddress()是该文件中的函数

#4


1  

you can make your file "somefile.php" organized as follows:

你可以使你的文件“somefile.php”组织如下:

function func1(){....}
function func2(){....}
function func3(){....}
....
foreach ($argv AS $arg){
    function_exists($arg) AND call_user_func($arg);
}

Then from command line or Linux cronjob, you run the following command

然后从命令行或Linux cronjob运行以下命令

php /path/to/somefile.php arg1 arg2 arg3 ...