Ad space (banner)
๐Ÿ˜PHP Lessons
Lesson 55 / 68

Reading Command-Line Arguments

In this lesson you will learn to read command-line arguments with $argv so you can build command-line tools. This is for anyone searching for "PHP command-line arguments how to use".

When you run PHP from the command line, the $argv array holds the arguments ($argv[0] is the script name itself). The example assumes you run it as php script.php Alice 14.

The sample code takes a name and an age from $argv[1] and $argv[2], using the ?? operator to supply defaults when no argument was passed. Remember that $argv[0] holds the script file name.

A common stumbling block for beginners is confusing execution through a web browser ($_GET, $_POST) with execution from the command line ($argv). PHP has both faces: web development and command-line scripting.

It is the essential mechanism for batch jobs and tools you run from a terminal. It also serves as the basis of simple automation scripts that branch on the values supplied at run time.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run PHP directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)