Categories
PHP Tips & Tutorials

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.

By Yang Yang

Hello, I'm Yang. I build online businesses that please people. Want to join in and post some useful articles on Kavoir.com? Shoot me a message.

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

Comments are closed.