Example code of the difference between JS and PHP in passing variable parameters to functions

PHP中文网
Release: 2023-02-28 19:46:01
Original
1085 people have browsed it

# JS method of calling a function to pass variable parameters

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

PHP method of calling a function to pass variable parameters

<?php 
  //方法一 
  //接收一系列参数,并逐一输出 
  function show_params () { 
    //获取传递参数的个数 
    $count = func_num_args(); 
    //遍历参数并逐一输出 
    for ($i = 0; $i < $count; $i++) { 
      //获取参数 
      $param = func_get_arg($i); 
      echo $param . PHP_EOL; 
    } 
  } 
  //调用函数 
  show_params(1, 2, &#39;apple&#39;, 3.14); 
  //方法2 
  function show_params () { 
    //定义存储传递参数的数组 
    $params = array(); 
    //获取全部参数 
    $params = func_get_args(); 
    $count = count($params); 
    //遍历并逐一输出参数 
    for ($i = 0; $i < $count; $i++) { 
      echo $params[$i]; 
      echo PHP_EOL; 
    } 
  } 
 //注: 方法2比方法1执行的慢一些
Copy after login

Related articles:

php How to pass each element of the array as the actual parameter of the variable parameter function?

php variable parameters

php variable parameter implementation

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!