An Alternative to Perl: Shell Scripting With PHP - Using Arguments in Scripts (Page 3 of 4 )
As you all know, we can use arguments with PHP (remember, world.php was an argument to the PHP interpreter). Similarly, we can use arguments in our scripts too. Curious? Let's take a look...
All of the arguments passed to your script are stored in a zero based global array variable called $argv. There is also another global variable, $argc, which holds number of arguments passed to the script (For all of those coming from a C/C++ background, this should be familiar to you).
One thing you need to remember is that $argc will always be one or more – never zero. This is because the name of the script being interpreted is always the first argument to the PHP interpreter. Here's the code, which will display the total number of arguments passed. It will also will display what those arguments are arguments:
Assuming this code is entered into a file called argument.php, you could test this script by running something like this:
php argument.php arg1 arg2
Here's the output:
Using PHP Script Instead of Perl Scripts As you must have guessed so far, the PHP executable can run independently from the web server. If you want to run your PHP scripts instead of Perl scripts -- which should be transparent to the system – you need to add a shebang to the top of the PHP script that you want to execute. A shebang looks like this:
This will tell the UNIX system that it needs to run the PHP interpreter for the following script, and the good thing is that this line will be ignored when you run the script under a Windows environment, thus you can write PHP scripts that are not operating system specific:
// your PHP code // goes here
Don't forget to add the PHP tags in the script file, otherwise PHP will not interpret it properly. If you want to suppress the PHP headers, use #!/usr/bin/php –q. Similarly you can also use any other PHP arguments that we discussed earlier by specifying them after the shebang.
Configuring Windows to Executing PHP Scripts If you plan to run PHP scripts from a command line on a Windows machine, then you need to associate the PHP files with the PHP interpreter. To do this, open Windows explorer, click on the tools menu and select folder options. Next, click on the file types tab and select the new button. Type .php into the file extension box and click OK:
You now need to select the PHP entry in the registered file types list box and click on the advanced button. Click new and type "Run" in the action box. In the "Application Used to Perform Actions" box, type C:\PHP\PHP.exe "%1" %* (change the PHP path if its different on your machine, %* is used to send any command line arguments). Click OK, and then OK again and then the close button:
With this step completed, Windows is now configured to run PHP Scripts. To try it out, just double click on any PHP file using Windows Explorer to see it running.
You can also register files with different PHP extensions, such as .php3 and .php4 using the same method mentioned above.