Call a PHP function from the command line

后端 未结 6 2097
野性不改
野性不改 2020-12-29 19:13

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

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-29 19:47

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

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

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

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

提交回复
热议问题