Dealing with PHP command line programs / scripts run on shell and the arguments

PHP is a lot more than just HTML rendering and outputting. It can also be run from shell command line like any legacy command line programs or tools we had in DOS and Linux shell.

One of the major problem is to deal with arguments passed to such program and how to identify them inside the PHP program code so we can actually use it. Because versatile parameters will surely make a program useful.

Solution

Within the program global scope, $argc contains the count or number of passed to the php program and $argv contains the value of them.

Note that the name of the php script is also passed in the arrays, therefore, $argc is larger than or equal to 1 and $argv[0] is the name of the php program that is being run.

1 thought on “Dealing with PHP command line programs / scripts run on shell and the arguments”

  1. Pingback: PHP: Read from Keyboard - Get User Input from Keyboard Console by Typing

Comments are closed.

Scroll to Top