It's common to pass programs options on the command line to modify their behaviour. You can do this with your CLI programs as well. The PHP CLI comes with two special variables designed specifically for this purpose: the $argv array, which stores the options passed to the PHP script on the command line as individual array elements, and the $argc variable, which stores the number of elements in the $argv array.
It's simple to write code within your PHP script to read $argv and process the options it contains. Try the illustrative script in Listing B to see how it works:
Listing B<?php
print_r($argv);
?>
Run this script by passing it some arbitrary values, and check the output:
shell> /path/to/phptest.php chocolate 276 "killer tie, dude!"
Array
(
[0] => test.php
[1] => chocolate
[2] => 276
[3] => killer tie, dude!
)
As you can see from the output, the values passed to test.php automatically appear in $argv as array elements. Notice that the first argument to $argvis always the name of the script itself.
Here's another, more advanced example: (Listing C)
Listing C<?php
// check for all required arguments
// first argument is always name of script!
if ($argc != 4) {
die("Usage: book.php <check-in-date> <num-nights> <room-type>\n");
}
// remove first argument
array_shift($argv);
// get and use remaining arguments
$checkin = $argv[0];
$nights = $argv[1];
$type = $argv[2];
echo "You have requested a $type room for $nights nights, checking in on $checkin. Thank you for your order!\n";
?>
Here's an example of its usage:
shell> /path/to/phpbook.php 21/05/2005 7 singleYou have requested a single room for 7 nights, checking in on 21/05/2005. Thank you for your order!
Here, the script first checks the $argc to ensure that the required number of arguments are present. It then extracts each argument from $argv and prints them back out to the standard output device.
Note: You can add more sophisticated command-line option parsing to PHP with theConsole_Getopt PEAR class.
Using CLI options
In addition to passing your PHP script options on the
command line, you can also pass the PHP CLI options to alter its behaviour. Table B contains a list of the
important ones:
Table B
|
Option |
Description |
| -a |
Run interactively |
| -c path |
Read php.ini file from path |
| -n |
Run without reading php.ini |
| -m |
List compiled modules |
| -i |
Display information about the PHP build |
| -l |
Syntax-check a PHP script |
| -s |
Display color-coded source |
| -w |
Display source code after stripping comments |
| -h |
Display help |
Interactive use
You can also use the PHP CLI interactively, typing in
commands and receiving an immediate response. To see this in action, invoke the
CLI executable without any options, as below:
shell> /path/to/php -a
You will be presented with a blank line, at which you can type in PHP code. Take a look:
shell> /path/to/php -aInteractive mode enabled
<?php
echo mktime();
1121187283
echo 2+2;
4
exit();
shell>
Alternatively, you can invoke the CLI without the -a parameter, and type in a complete script or code block. Use <Ctrl>-D to end the code block and have the CLI execute it. Here's an example:
shell> /path/to/php<?php
echo date("d-M-Y h:i:s", time());
?>
<Ctrl-D>
12-Jul-2005 06:54:04
And that's about it! You should now know enough about the PHP CLI to begin using it yourself. Have fun, and happy coding!
Do you need help with PHP? 



1
dsqfdsdsq - 30/01/07
can someone help me
» Report offensive content
2
Baho - 23/02/07
Hey guys, whats wrong in this code?
Inputs are: 1, 2, 3
Output is:" ", 2, " ", 3, " "
While it was expected to show, just 1, 2, 3 in seperate lines.
Could u just find a little time and send me the solution to beemzet@gmail.com?
» Report offensive content
3
james - 15/05/07
Baho, you've declared the $S variable, but in your echo statement, you're using $s.
$S and $s are different because of their case.
» Report offensive content
4
Vijay - 09/01/08
can anyone help me to solve my problem.
i want to invoke my putty.exe using php script. when I click in the link, then putty will open and waiting for password to enter. I want to set ip address and username also.
I have tried this script
WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("putty.exe", 4, true);
but didn't get result
Please help me
» Report offensive content
5
phphelp - 05/04/08
Wow, thanks for your code!
I had a non-working code & with a minor change (got from your site), it works!!!
Thanks.
» Report offensive content
6
Rafael - 10/05/08
Vjay,
you are missing the dollar sign in front of your first variable declaration.
Best.
» Report offensive content
7
Virus - 25/07/08
I don't get any output on my computer with any thing from a .php file if i runt php -r 'some code' it works but nothing else...
» Report offensive content
8
ana_sh0otz - 20/08/08
i want subscribe my website comment using php but i dont know what the related command and how the comment can view in my website.
» Report offensive content