Create a simple text file that contains the following PHP code and save it as hello.php:
Copy CodeThe code is as follows:
echo "Hello from the CLI";
?>
Now, try running the program at a command-line prompt by invoking the CLI executable and providing the script's file name:
#php phphello.php
Output Hello from the CLI
Use of standard inputs and outputs
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 CodeThe code is as follows:
Ask for input
Fwrite (STDOUT, "Enter Your Name:");
Get input
$name = Trim (fgets (STDIN));
Write input back
Fwrite (STDOUT, "Hello, $name!");
?>
Look how happens when 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 for the user's name. It then reads the user input information obtained from the standard input device
Takes a PHP variable and merges it into a string. The string is then printed out to the standard output device using fwrite ().
-----------------using command-line arguments
It is common practice to enter program parameters at the command line to change how they are run. You can also do this to the CLI program. The PHP CLI has two special variables specifically designed to achieve this
Purpose: One is the $ARGV variable, which saves the arguments passed to the PHP script as a separate array element through the command line, and the other is the $ARGC variable, which is used to hold the elements of the $ARGV array.
Number.
It is very simple to write a piece of code that reads $ARGV and processes the parameters contained in a PHP script. Try the example script in list B to see how it works:
List B
Copy CodeThe code is as follows:
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, the value passed to the test.php is automatically shown as an array element in the $ARGV. It is important to note that the first argument of a $argvis is always
The name of the script itself.
The following is a more complex example (List C):
List C
Code
Copy CodeThe code is as follows:
Check for all required arguments
First argument is always name of script!
if ($ARGC! = 4) {
Die ("Usage:book.php ");
}
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 the $nights nights, checking in on $checkin. Thank for your order! ";
?>
Here is an example of its usage:
shell> PHP phpbook.php 21/05/2005 7 single
You are requested a single and 7 nights, checking in 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
http://www.bkjia.com/PHPjc/321200.html www.bkjia.com true http://www.bkjia.com/PHPjc/321200.html techarticle Create a simple text file that contains the following PHP code and save it as hello.php: Copy the code as follows:? php echo "Hello from the CLI";? Now, try to command ...