Command Line Arguments in PHP



In PHP, all command line arguments, including the script name itself are stored in a special array named $argv. The first element, zero, is the name of the script being run. For example:

echo "This script being executed is named " . $argv[0] . ".\n";

If you run

php myScript.php

The output will be

This script being executed is named test.php.

The output will be the same if you begin the file with a shebang like #!/usr/bin/php and run the script by typing


All subsequent arguments will be in the $argv array.