Create a simple text file that contains the following PHP code and save it as hello.php:
Copy Code code as follows:
<?php
echo "Hello from the CLI";
?>
Now, try running this program at a command-line prompt by calling the CLI executable and providing the file name of the script:
#php phphello.php
Output Hello from the CLI
Use standard input and output
You can use these three constants in your PHP scripts to accept user input, or to display the results of processing and calculations. To better understand this, take a look at the following script (
List A):
List A
Copy Code code as follows:
<?php
Ask for input
Fwrite (STDOUT, "Enter Your Name:");
Get input
$name = Trim (fgets (STDIN));
Write input back
Fwrite (STDOUT, "Hello, $name!");
?>
Look what happens if you run it:
shell> PHP hello.php
Enter your Name:joe
Hello, joe!.
In this script, the fwrite () function first writes a message to the standard output device, asking the user's name. It then reads the user input information obtained from the standard input device
into a PHP variable, and it takes the merge into a string. The string is then printed to the standard output device using fwrite ().
-----------------using command line arguments
It is common practice to enter program parameters on the command line to change how they are run. You can also do this with the CLI program. The PHP CLI comes with two special variables that are designed to achieve this
Objective: One is the $ARGV variable, which saves the argument passed to the PHP script as a separate array element via the command line, and the other is the $ARGC variable, which holds the elements in the $ARGV array.
Number.
It is easy to write a section of code that reads $ARGV and processes the parameters it contains with a PHP script. Try the sample script in list B to see how it works:
List B
Copy Code code as follows:
<?php
Print_r ($ARGV);
?>
Run this script by passing it some arbitrary values, and check the output:
shell> php 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 results, the values passed to test.php automatically appear as array elements in the $ARGV. Note that the first argument $argvis is always
The script's own name.
The following is a more complex example (List C):
List C
Code
Copy Code code as follows:
<?php
Check for all required arguments
The argument is always name of script!
if ($ARGC!= 4) {
Die ("Usage:book.php <check-in-date> <num-nights> <room-type>");
}
Remove-A-argument
Array_shift ($ARGV);
Get and use remaining arguments
$checkin = $argv [0];
$nights = $argv [1];
$type = $ARGV [2];
echo "You have requested a $type the room for $nights nights, the checking in on $checkin. Thank for your order! ";
?>
The following is an example of its usage:
shell> PHP phpbook.php 21/05/2005 7 single
You are have requested a single room for 7 nights, checking into on 21/05/2005. Thank for your order!
Here, the script first checks the $ARGC to ensure that the number of independent variables meets the requirements. It then extracts each of the arguments from the $argv and prints them out to the standard output