Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Files and I/O

PHP $argv, $argc and getopt()

Reference for PHP command-line arguments: the $argv and $argc variables, getopt() short and long options, and how to validate and parse arguments safely.

When you run a script from a terminal, everything after the file name is passed to it as arguments. PHP stores them in two variables: $argv, an array of strings whose first element is the script name, and $argc, the number of elements. The same data is available as $_SERVER['argv'] and $_SERVER['argc'].

For options such as -v or --name=Ada, the function getopt() parses $argv for you and returns an array of what it found.

PHP
<?php
// php report.php daily --limit=10
echo $argc;        // 3
echo $argv[1];     // daily

The examples here run with no arguments, so they show the empty case and simulate arguments with arrays. The read-only snippets show what a real terminal call would print.

Try it

PHP What a script sees with no arguments

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Parsing a simulated argument list

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
$argv: array<int, string>
$argc: int
getopt(string $short_options, array $long_options = [], &$rest_index = null): array|false
Name Description
$short_options Letters of single-character options. A colon after a letter means a required value (f:), two colons an optional value (f::).
$long_options List of long option names, with the same colon rules ('name:', 'level::').
$rest_index Set to the index in $argv where option parsing stopped, so you can find the positional arguments after the options.

Return value: an array keyed by option name. Options without a value get false; repeated options give an array of values. Returns false on failure.

Errors / Exceptions: none. Unknown options are silently ignored by getopt().

Read-only example of a real call:

Terminal
php tool.php -v --name=Ada input.txt
# getopt('v', ['name:'], $rest) returns ['v' => false, 'name' => 'Ada']
# $rest is 3, and $argv[$rest] is 'input.txt'

Usage notes

  • $argv[0] is the script name as typed, so real arguments start at index 1.
  • Everything is a string. Convert numbers with (int) or filter_var() and validate before using them.
  • Availability. $argv and $argc exist in the global scope of CLI scripts. Inside a function use $GLOBALS['argv'] or $_SERVER['argv']. In web requests they exist only if register_argc_argv is on.
  • getopt() stops at the first non-option. Everything after it, such as input.txt, must be handled using $rest_index.
  • Option values: -n Ada, -nAda and --name=Ada all work for a required value, but --name Ada (with a space) is accepted only for required values; optional values must be attached with =.
  • Validate and give feedback. Print a usage message to STDERR and exit with a non-zero status for bad input. See exit and die.
  • Never pass user arguments straight to a shell. Use escapeshellarg() if you must call another program.
  • For larger tools use a console library; hand-rolled parsing gets complicated with subcommands.