Create a simple text file containing the following PHP code and save it as hello.php:
Copy the code The code is as follows:
echo "Hello from the CLI";
?>
Now, try running this program from the command line prompt, method is the file name that calls the CLI executable and provides the script:
#php phphello.php
Output Hello from the CLI
Using standard input and output
You can Use these three constants in your own PHP script to accept user input or display the results of processing and calculation. To understand this better, take a look at the following script (
Listing A):
Listing A
Copy code The 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 what happens when you run it:
shell> php hello.php
Enter your name: Joe
Hello, Joe!
In this script , the fwrite() function will first write a message to the standard output device and ask for the user's name. Then it will read the user input information obtained from the standard input device
into a PHP variable, and combine it into a string. Then use fwrite() to print the string to the standard output device.
-----------------Using command line arguments
It is very common to enter program parameters on the command line to change the way it runs. practice. You can do this with CLI programs as well. PHP CLI comes with two special variables specifically designed to achieve this
purpose: one is the $argv variable, which saves the parameters passed to the PHP script through the command line as separate array elements; the other is The $argc variable is used to save the
number of elements in the $argv array.
It is very simple to write a piece of code in PHP script that reads $argv and processes the parameters it contains. Try the example script in Listing B and see how it works:
Listing B
Copy the code The 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 test.php Will automatically appear in $argv as an array element. Note that the first argument to $argvis is always
the name of the script itself.
Here is a more complex example (Listing C):
Listing C
Code
Copy code The 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 room for $nights nights, checking in on $checkin. Thank you for your order! ";
?>
Here is an example of its usage:
shell> php phpbook.php 21/05/2005 7 single
You have requested a single room for 7 nights, checking in on 21/05/2005. Thank you for your order!
Here, the script first checks $argc to ensure that the number of arguments meets the requirements. It then extracts each argument from $argv and prints them to standard output.
http://www.bkjia.com/PHPjc/321200.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321200.htmlTechArticleCreate a simple text file containing the following PHP code and save it as hello.php: Copy The code is as follows: ?php echo "Hello from the CLI"; ? Now, try the command...