Home > Article > Backend Development > Example code of the difference between JS and PHP in passing variable parameters to functions
# 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, 'abc'); </script>
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, 'apple', 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执行的慢一些
Related articles: