Home > php教程 > php手册 > body text

JS与PHP向函数传递可变参数的区别实例代码

WBOY
Release: 2016-06-13 12:09:49
Original
1102 people have browsed it

# JS 调用函数传递可变参数的方法

复制代码 代码如下:


<script> <BR>function test() { <BR>   for(var i = 0;i < arguments.length; i++) { <BR>   alert(arguments[i]); <BR>  } <BR>} <BR>//调用函数 <BR>test(1, 2, 3, 'abc'); <BR></script>


# PHP 调用函数传递可变参数的方法

复制代码 代码如下:


  //方法一
  //接收一系列参数,并逐一输出
  function show_params () {
    //获取传递参数的个数
    $count = func_num_args();

    //遍历参数并逐一输出
    for ($i = 0; $i       //获取参数
      $param = func_get_arg($i);
      echo $param . PHP_EOL;
    }
  }

  //调用函数
  show_params(1, 2, 'apple', 3.14);

  //方法2
  function show_params () {
    //定义存储传递参数的数组
    $params = array();
    //获取全部参数
    $params = func_get_args();
    $count = count($params);
    //遍历并逐一输出参数
    for ($i = 0; $i       echo $params[$i];
      echo PHP_EOL;
    }
  }
 //注: 方法2比方法1执行的慢一些

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template