The difference between JS and PHP that pass variable arguments to a function
- 2020-05-07 19:18:46
- OfStack
# JS calls a method that passes a variable argument to the function
# PHP calls the method that passes the variable argument to the function
<script>
function test() {
for(var i = 0;i < arguments.length; i++) {
alert(arguments[i]);
}
}
// Call a function
test(1, 2, 3, 'abc');
</script>
# PHP calls the method that passes the variable argument to the function
<?php
// methods 1
// receive 1 Series of parameters, and by 1 The output
function show_params () {
// Gets the number of passed parameters
$count = func_num_args();
// Iterate over the parameters and proceed 1 The output
for ($i = 0; $i < $count; $i++) {
// To obtain parameters
$param = func_get_arg($i);
echo $param . PHP_EOL;
}
}
// Call a function
show_params(1, 2, 'apple', 3.14);
// methods 2
function show_params () {
// Defines an array to store passed parameters
$params = array();
// Get all parameters
$params = func_get_args();
$count = count($params);
// Traversal and one by one 1 Output parameters
for ($i = 0; $i < $count; $i++) {
echo $params[$i];
echo PHP_EOL;
}
}
// note : methods 2 Than the method 1 Perform slow 1 some