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

by Yang Yang on April 22, 2009

Share This Article:
Subscribe to Kavoir: blog feed

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.

Share This Article:
Subscribe to Kavoir: blog feed

You should also read:

Leave a Comment

{ 1 trackback }

Previous post:

Next post: