test.php:

Copy<?php
print_r($argv);
?>

Shell:

Copy$ php -q test.php foo bar
Array
(
    [0] => test.php
    [1] => foo
    [2] => bar
)
Answer from schneck on Stack Overflow
🌐
PHP
php.net › manual › en › reserved.variables.argv.php
PHP: $argv - Manual
To test if a script is being run from the command line, php_sapi_name() should be used instead of checking whether $argv or $_SERVER['argv'] is set. ... array(4) { [0]=> string(10) "script.php" [1]=> string(4) "arg1" [2]=> string(4) "arg2" [3]=> string(4) "arg3" } ... This is also available as $_SERVER['argv']. getopt() - Gets options from the command line argument list ... Please note that, $argv and $argc need to be declared global, while trying to access within a class method.
🌐
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 - Argument #2 - value2 Note that the 0th argument is the name of the PHP script that is run. The rest of the array are the values passed in on the command line. The values are accessed via the $argv array. This approach works, but it is very simplistic and doesn't play well if you're looking to transition from a query parameter way of passing in values to your script. With ...
🌐
A Star Trek Timeline
macs.hw.ac.uk › ~hwloidl › docs › PHP › features.commandline.html
Chapter 23. Using PHP from the command line
In the script above, we used the special first line to indicate that this file should be run by PHP. We work with a CLI version here, so there will be no HTTP header printouts. There are two variables you can use while writing command line applications with PHP: $argc and $argv. The first is ...
🌐
BCCNsoft
doc.bccnsoft.com › docs › php-docs-7-en › features.commandline.usage.html
Executing PHP files
If not, or if the argument was --help , -help , -h or -? , the help message is printed out, using $argv[0] to dynamically print the script name as typed on the command line. Otherwise, the argument is echoed out exactly as received. To run the above script on Unix, it must be made executable, and called simply as script.php echothis or script.php -h.
🌐
Jarrodoberto
jarrodoberto.com › articles › 2011 › 12 › running-php-from-the-command-line-basics
Running PHP from the Command Line - Basics
To do something useful with this in PHP we use the array variable $argv. This is simply an array, created by PHP, that contains our arguments. if (isset($argv[1])) { echo 'My name is ' . $argv[1]; } Notice we used the array index 1 and not 0? That is because $argv[0] is always the name that ...
🌐
Ask-sheldon
ask-sheldon.com › home › run php script with parameters within another php script
Run PHP script with parameters within another PHP script - Ask Sheldon Tech Blog
November 4, 2016 - //set arguments $_SERVER['argv'][1] = '-action'; $_SERVER['argv'][2] = 'unsetMaintenanceMode'; $_SERVER['argv'][3] = '-initRedis'; $_SERVER['argv'][4] = '1'; //call script unset($_SERVER['REQUEST_METHOD']); include(dirname(dirname(__FILE__)) ...
Find elsewhere
🌐
Envato Tuts+
code.tutsplus.com › home › coding fundamentals
Get Command-Line Arguments With PHP $argv or getopt() | Envato Tuts+
December 19, 2021 - There are two predefined variables in PHP, called $argc and $argv, that you can use to work with command-line arguments. The variable $argc simply tells you the number of arguments that were passed to the script.
🌐
DEV Community
dev.to › gbhorwood › writing-command-line-scripts-in-php-part-1-3jpb
writing command line scripts in php: part 1; args, preflighting and more - DEV Community
March 15, 2023 - traditionally, when running a php script on the command line, you invoke the php command and pass the script as an argument, ie php /path/to/my/script.php. this works, but is ugly. to fix this, we'll be putting a shebang at the top of our php ...
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-execute-php-code-using-command-line
How to Execute PHP Code using Command Line? - GeeksforGeeks
Navigate to the directory containing the file. ... You can execute a small PHP code snippet directly from the command line without needing a script file. ... You can pass arguments to your script using the $argv array.
Published   July 12, 2025
🌐
JetBrains
jetbrains.com › help › phpstorm › run-debug-configuration-php-script.html
PHP Script | PhpStorm Documentation
In this area, specify the script to run or debug and the parameters to process it with, if applicable. In this area, customize the behavior of the current PHP interpreter by specifying the options and arguments to be passed to the PHP executable file.
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