Call a PHP function from the command line

后端 未结 6 2088
野性不改
野性不改 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:43

    As of PHP 5.1.0 (7.1.0 on windows) PHP can be executed from the shell by running

    php -a
    

    This starts PHP's interactive shell. More info here.

    With PHP's shell, running you can require files the same way you would from a file.

    So if the previous command was run from the folder containing the file:

    php > require 'myFile.php';
    

    If it is in a subfolder:

    php > require 'path/to/file/myFile.php';
    

    Then execute any function defined in myFile.php.

    php > myFunction(myParamsIfAny);
    

    I guess you can use any variable defined in the same file or require any other file containing the variables needed, although I haven't tried it.

提交回复
热议问题