test.php:

<?php
print_r($argv);
?>

Shell:

$ php -q test.php foo bar
Array
(
    [0] => test.php
    [1] => foo
    [2] => bar
)
Answer from schneck on Stack Overflow
🌐
Igor's Blog
igorkromin.net › index.php › 2017 › 12 › 07 › how-to-pass-parameters-to-your-php-script-via-the-command-line
How to pass parameters to your PHP script via the command line | Igor Kromin
December 7, 2017 - $val = getopt(null, ["name:"]); When using long parameter names, the way that the PHP script is run changes slightly, in this case we pass the parameter like so... ... ) This can be expanded to multiple differently named parameters too.
🌐
PHP
php.net › manual › en › function.exec.php
PHP: exec - Manual
If you want to execute a command in the background without having the script waiting for the result, you can do the following: <?php passthru("/usr/bin/php /path/to/script.php ".$argv_parameter." >> /path/to/log_file.log 2>&1 &"); ?> There are a few thing that are important here.
Find elsewhere
🌐
BCCNsoft
doc.bccnsoft.com › docs › php-docs-7-en › features.commandline.usage.html
Executing PHP files
$ some_application | some_filter | php | sort -u > final_output.txt You cannot combine any of the three ways to execute code. As with every shell application, the PHP binary accepts a number of arguments; however, the PHP script can also receive further arguments.
🌐
Envato Tuts+
code.tutsplus.com › home › coding fundamentals
Get Command-Line Arguments With PHP $argv or getopt() | Envato Tuts+
December 19, 2021 - In this tutorial, you will learn how to access command-line arguments inside your script with $argv and getopt() in PHP. Arguments are passed around as query parameters when you request ...
Top answer
1 of 2
1

If you want to execute this script from PHP as well as from Java, then I'd take this approach:

  • put your code which does the work into a function within the file "myfunc.php" (give it a more meaningful name). Make the function take whatever arguments it needs, but it shouldn't try to accss $_GET parameters because these will only exist when called through the web.
  • now create a PHP file which includes "myfunc.php" and calls the function. This file can access $_GET and other web-specific variables, passing them as parameters to your function.
  • To also be able to execute from Java, create a 3rd file called "mycommand.php", which you can execute from the command line. If you need to pass command line arguments, see documentation on $argv. This script should include "myfunc.php" and call the function, passing any necessary parameters.

Develop and test this last script by running your script from the command line:

$ php5 mycommand.php <args go here>

After you get it working like this, then you can invoke it from java by using this solution.

There is no reason to invoke a PHP command as a subprocess from a PHP script - using "include" as I've described above is easier and more efficient. However here is an example for testing purposes:

test.php (invoke this through the browser):

<?
exec("php5 test2.php", $ret);
foreach ($ret as $line) {
  print $line . '<br>';
}
?>

This script invokes a second PHP script called "test2.php" (in the same directory), and prints the output from that command.

test2.php simply produces some output:

<?  
print "foo\n"; 
print "bar\n"; 
print "glorp\n"; 
?>
2 of 2
1

If you are feeding the php exec command with a variable, and that variable is coming from some form of user input, you can get yourself into a heap of trouble. A user could input a command that really messes with your system.

As for the &&2>&1, see

In the shell, what does " 2>&1 " mean?

These are redirect commands used in a bourne shell (not javascript that I know of) to send the stdout or stderr to a designated, not-normal place.

R

Top answer
1 of 2
241

Presumably you're passing the arguments in on the command line as follows:

php /path/to/wwwpublic/path/to/script.php arg1 arg2

... and then accessing them in the script thusly:

<?php
// $argv[0] is '/path/to/wwwpublic/path/to/script.php'
$argument1 = $argv[1];
$argument2 = $argv[2];
?>

What you need to be doing when passing arguments through HTTP (accessing the script over the web) is using the query string and access them through the $_GET superglobal:

Go to http://yourdomain.example/path/to/script.php?argument1=arg1&argument2=arg2

... and access:

<?php
$argument1 = $_GET['argument1'];
$argument2 = $_GET['argument2'];
?>

If you want the script to run regardless of where you call it from (command line or from the browser) you'll want something like the following:

as pointed out by Cthulhu in the comments, the most direct way to test which environment you're executing in is to use the PHP_SAPI constant. I've updated the code accordingly:

<?php
if (PHP_SAPI === 'cli') {
    $argument1 = $argv[1];
    $argument2 = $argv[2];
}
else {
    $argument1 = $_GET['argument1'];
    $argument2 = $_GET['argument2'];
}
?>
2 of 2
18
$argv[0]; // the script name
$argv[1]; // the first parameter
$argv[2]; // the second parameter

If you want to all the script to run regardless of where you call it from (command line or from the browser) you'll want something like the following:

<?php
if ($_GET) {
    $argument1 = $_GET['argument1'];
    $argument2 = $_GET['argument2'];
} else {
    $argument1 = $argv[1];
    $argument2 = $argv[2];
}
?>

To call from command line chmod 755 /var/www/webroot/index.php and use

/usr/bin/php /var/www/webroot/index.php arg1 arg2

To call from the browser, use

http://www.mydomain.example/index.php?argument1=arg1&argument2=arg2
Top answer
1 of 9
59

When calling a PHP script from the command line you can use $argc to find out how many parameters are passed and $argv to access them. For example running the following script:

<?php
    var_dump($argc); //number of arguments passed 
    var_dump($argv); //the arguments passed
?>

Like this:-

php script.php arg1 arg2 arg3

Will give the following output

int(4)
array(4) {
  [0]=>
  string(21) "d:\Scripts\script.php"
  [1]=>
  string(4) "arg1"
  [2]=>
  string(4) "arg2"
  [3]=>
  string(4) "arg3"
}

See $argv and $argc for further details.

To do what you want, lets say

php script.php arg1=4

You would need to explode the argument on the equals sign:-

list($key, $val) = explode('=', $argv[1]);
var_dump(array($key=>$val));

That way you can have whatever you want in front of the equals sign without having to parse it, just check the key=>value pairs are correct. However, that is all a bit of a waste, just instruct the user on the correct order to pass the arguments.

2 of 9
27

I use this fairly concise method:

if($argc>1)
  parse_str(implode('&',array_slice($argv, 1)), $_GET);

Which would handle a call such as:

php script.php item1=4 item2=300

By sending it into $_GET you automatically handle web or CLI access.

For commentary, this is doing the following:

  • If the count of arguments is greater than one (as first item is the name of the script) then proceed
  • Grab the arguments array excluding first item
  • Turn it into a standard query string format with ampersands
  • use parse_str to extract to the $_GET array