How to read command line parameters in PHP Shell Scripts

We can access parameters passed via the command line in our PHP shell scripts. Those are stored as an array in the variable $argv. Consider this:

The first part of the script prints out all parameters that have been given, while the second part checks if the parameter was “x” or not. Note that the first item in the array ($argv[0]) will be the the first item on the command line, i.e. the file name and path to this very script. $argv[1] is the first parameter, $argv[2] the second, and so forth.

We can call the script with

to give it one parameter, or with

to give it three parameters.

  • http://php.net/manual/en/reserved.variables.argv.php

Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.

Add your voice!