怎么建设只要注册就赚钱的网站,免费推广的app有哪些,做网站建设公司crm在线的提升服务,重庆网站建设模板服务如何在命令行下运行PHP脚本[带参数] 创建一个简单的文本文件#xff0c;其中包含有以下PHP代码#xff0c;并把它保存为hello.php#xff1a;?phpecho Hello from the CLI;?现在#xff0c;试着在命令行提示符下运行这个程序#xff0c;方法是调用C…如何在命令行下运行PHP脚本[带参数] 创建一个简单的文本文件其中包含有以下PHP代码并把它保存为hello.php ?php echo Hello from the CLI; ? 现在试着在命令行提示符下运行这个程序方法是调用CLI可执行文件并提供脚本的文件名 #php phphello.php 输出Hello from the CLI -----------------使用标准的输入和输出 你可以在自己的PHP脚本里使用这三个常量以接受用户的输入或者显示处理和计算的结果。要更好地理解这一点可以看看下面的脚本 列表A 列表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! 在这个脚本里fwrite()函数首先会向标准的输出设备写一条消息询问用户的姓名。然后它会把从标准输入设备获得的用户输入信息读 取到一个PHP变量里并它把合并成为一个字符串。然后就用fwrite()把这个字符串打印输出到标准的输出设备上。 -----------------使用命令行自变量 在命令行里输入程序参数来更改其运行方式是很常见的做法。你也可以对CLI程序这样做。PHP CLI带有两个特殊的变量专门用来达到这个 目的一个是$argv变量它通过命令行把传递给PHP脚本的参数保存为单独的数组元素另一个是$argc变量它用来保存$argv数组里元素的 个数。 用PHP脚本编写一段读取$argv并处理它所含参数的代码是很简单的。试试列表B里的示例脚本看看它是如何工作的 列表B ?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! ) 正如你可以从输出的结果看到的传递给test.php的值会自动地作为数组元素出现在$argv里。要注意的是$argvis的第一个自变量总是 脚本自己的名称。 下面是一个更加复杂的例子列表C 列表C 代码 ?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! 在这里脚本首先会检查$argc以确保自变量的数量符合要求。它然后会从$argv里提取出每一个自变量把它们打印输出到标准的输出。 from:http://www.cnblogs.com/Lovepanda/archive/2010/01/19/1651897.html转载于:https://www.cnblogs.com/dkblog/archive/2010/06/04/1980695.html