47

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jack Smit
  • 3,093
  • 4
  • 30
  • 45

8 Answers8

99

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.

Tim S.
  • 13,597
  • 7
  • 46
  • 72
  • 2
    php --run "require 'address.php'; exportAddress(12345);" which is easy to remember to call from CLI – Tarik Apr 01 '17 at 11:12
  • @Tarik what could you do if the parameter you need to pass in is an Oracle Database Connection which is the output from another function? – Drew Aschenbrener Oct 06 '20 at 16:13
7

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user7282
  • 5,106
  • 9
  • 41
  • 72
6

Add this to the top of the file "/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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Simon Rodan
  • 81
  • 1
  • 4
5

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 ...
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Samer Ata
  • 1,027
  • 1
  • 12
  • 11
3

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.

Alobar
  • 63
  • 1
  • 7
1

To extend on Samer Ata and Simon Rodan's answers, you can use the following to be able to execute any function with any amount of arguments:

if(isset($argv[1]) && function_exists($argv[1])) {
  $parameters = array_slice($argv, 2);
  call_user_func($argv[1], ...$parameters);
}

This is done by removing the script and function name from $argv and then using the spread operator (...) to interpret each element in the array as an argument to call_user_func.

The operator is supported from PHP 5.6 onward (and 5.5 can do some of the things using functions). You can read more about it in the official PHP documentation. For completeness, this is also known as argument unpacking.

Steen Schütt
  • 1,355
  • 1
  • 17
  • 31
0

Note that using php -r assumes php-cli is the binary that got installed as ‘php’. If php -v shows the ‘php’ is php-cgi, type /path/to/php/bin/php-cli -r instead. In case it’s not there, you’ll have to re-compile with cli enabled.

The doc has relevant pages on this.

-1

for call a specific function in a PHP file from the command line by using -r:

php -r 'include "address.php"; exportAddress("value");'

tip: "value" is the actual value that you want to pass to the exportAddress function.

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 02 '23 at 09:41
  • 1
    Why did you copy-pasted the solution already offered in other answers? – Your Common Sense Jun 02 '23 at 10:07