func_get_arg()
func_get_args() 
with func_num_args() <?php
function foo() 
{
     $numargs = func_num_args();
     echo "Number of arguments: $numargs<br />\n";
     if ($numargs >= 2) {
     echo "Second argument is: " . func_get_arg(1) . "<br />\n";
     }
} foo (1, 2, 3);
?> 
<?php
function foo() 
{
    $numargs = func_num_args();
    echo "Number of arguments: $numargs<br />\n";
    if ($numargs >= 2) {
        echo "Second argument is: " . func_get_arg(1) . "<br />\n";
    }
    $arg_list = func_get_args();
    for ($i = 0; $i < $numargs; $i++) {
        echo "Argument $i is: " . $arg_list[$i] . "<br />\n";
    }
} foo(1, 2, 3);
?>

解决方案 »

  1.   

    我没有说清楚 我的目的不是从函数中读出参数
    而是php testarg.php -dabc中
    带有d或者dabc的参数以便程序处理 能否再给解答以下
      

  2.   

    例子 23-1. 试图以命令行方式运行的 PHP 脚本(script.php)#!/usr/bin/php
    <?phpif ($argc != 2 || in_array($argv[1], array('--help', '-help', '-h', '-?'))) {
    ?>This is a command line PHP script with one option.  Usage:
      <?php echo $argv[0]; ?> <option>  <option> can be some word you would like
      to print out. With the --help, -help, -h,
      or -? options, you can get this help.<?php
    } else {
        echo $argv[1];
    }
    ?>  
     
    在以上脚本中,我们用第一行特殊的代码来指明该文件应该由 PHP 来执行。我们在这里使用 CLI 的版本,因此不会有 HTTP 头信息输出。在您用 PHP 编写命令行应用程序时,您可以使用两个参数:$argc 和 $argv。前面一个的值是比参数个数大 1 的整数(运行的脚本本身的名称也被当作一个参数)。第二个时包含有参数的数组,其第一个元素为脚本的名称,下标为数字 0($argv[0])。 在以上程序中我们检查了参数的个数是大于 1 个还是小于 1 个。即时参数是 --help、-help、-h 或 -?,我们仍然打印出帮助信息,并同时动态输出脚本的名称。如果还收到了其它参数,我们也把它们显示出来。 
      

  3.   

    稻草人 right
    thx 揭帖