时间:2021-07-01 10:21:17 帮助过:29人阅读
输出Hello from the CLI
- <br><?php <br>echo "Hello from the CLI"; <br>?> <br> <br>现在,试着在命令行提示符下运行这个程序,方法是调用CLI可执行文件并提供脚本的文件名: <br>#php phphello.php <br>
输出设备写一条消息,询问用户的姓名。然后它会把从标准输入设备获得的用户输入信息读
- <br><?php <br>// ask for input <br>fwrite(STDOUT, "Enter your name: "); <br><br>// get input <br>$name = trim(fgets(STDIN)); <br><br>// write input back <br>fwrite(STDOUT, "Hello, $name!"); <br>?> <br> <br>Look what happens when you run it: <br>shell> php hello.php <br>Enter your name: Joe <br>Hello, Joe! <br><br>在这个脚本里,fwrite()函数首先会向标准的
输出的结果看到的,传递给test.php的值会自动地作为数组元素出现在$argv里。要注意的是,$argvis的第一个自变量总是
- <br><?php <br>print_r($argv); <br>?> <br> <br>Run this script by passing it some arbitrary values, and check the output: <br><br>shell> php phptest.php chocolate 276 "killer tie, dude!" <br>Array <br>( [0] => test.php <br>[1] => chocolate <br>[2] => 276 <br>[3] => killer tie, dude! <br>) <br><br>正如你可以从
输出到标准的输出
- <br><?php <br>// check for all required arguments <br>// first argument is always name of script! <br>if ($argc != 4) { <br>die("Usage: book.php <check-in-date> <num-nights> <room-type> "); <br>} <br><br>// remove first argument <br>array_shift($argv); <br><br>// get and use remaining arguments <br>$checkin = $argv[0]; <br>$nights = $argv[1]; <br>$type = $argv[2]; <br>echo "You have requested a $type room for $nights nights, checking in on $checkin. Thank you for your order! "; <br>?> <br> <br><br>下面是其用法的示例: <br><br>shell> php phpbook.php 21/05/2005 7 single <br>You have requested a single room for 7 nights, checking in on 21/05/2005. Thank you for your order! <br><br>在这里,脚本首先会检查$argc,以确保自变量的数量符合要求。它然后会从$argv里提取出每一个自变量,把它们打印