如何在命令行下运行PHP脚本[带参数]
创建一个简单的文本文件,其中包含有以下PHP代码,并把它保存为hello.php:
<?php
echo "Hello from the CLI";
?>
现在,试着在命令行提示符下运行这个程序,方法是调用CLI可执行文件并提供脚本的文件名:
#php phphello.php
输出Hello from the CLI
列表A
<?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 when you run it:
shell> php hello.php
Enter your name: Joe
Hello, Joe!
代码
<?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> ");
}
// 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! ";
?>
下面是其用法的示例:
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!