Home Backend Development PHP Tutorial JS与PHP向函数传递可变参数的区别实例代码_PHP

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

Jun 01, 2016 pm 12:16 PM
transfer variable parameter

# 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执行的慢一些

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1248
24
What are Delivery Optimization files in Windows 11 and can you delete them? What are Delivery Optimization files in Windows 11 and can you delete them? Sep 29, 2023 pm 04:09 PM

Delivery Optimization is a feature that helps Windows Update and Windows Store run and deliver updates faster. Cache files in Delivery Optimization are supposed to be deleted after a while, but for some of our readers they keep piling up and taking up unnecessary space. Is it safe to delete delivery optimization files? Yes, it is safe to delete delivery optimization files, and in this article, you will find out how easy it is to do so in Windows 11. Although it is not recommended to manually delete delivery optimization files, it is possible to do so automatically. How to delete delivery optimization files on Windows 11? Click the search bar, type Disk Cleanup, and open the tool from the results. If you have multiple drives, select the drive with your system (usually C:

C++ function variable parameter passing mechanism C++ function variable parameter passing mechanism Apr 20, 2024 am 09:18 AM

The C++ variable parameter passing mechanism allows a function to accept an indefinite number of parameters. The syntax is to use the ... omission symbol to indicate variable parameters. Common applications include formatted output, such as the printf() function, which uses va_list to access the variable argument list.

How to optimize the use of default parameters and variadic parameters in C++ functions How to optimize the use of default parameters and variadic parameters in C++ functions Apr 20, 2024 am 09:03 AM

Optimize C++ default and variable parameter functions: Default parameters: Allow functions to use default values, reducing redundancy. Put default parameters last to improve readability. Use constexpr default parameters to reduce overhead. Use structured binding to improve readability of complex default parameters. Variable parameters: allows a function to accept a variable number of parameters. Try to avoid using variadic arguments and use them only when necessary. Use std::initializer_list to optimize variadic functions to improve performance.

Detailed explanation of how JavaScript functions implement variable parameters? (Summary sharing) Detailed explanation of how JavaScript functions implement variable parameters? (Summary sharing) Aug 04, 2022 pm 02:35 PM

JS is a weakly typed language and cannot use the param keyword to declare that the formal parameter is a variable parameter like C#. So how to implement this variable parameter in js? The following article will talk about the implementation method of JavaScript function variable parameters. I hope it will be helpful to everyone!

How to use context to pass request parameters in Go How to use context to pass request parameters in Go Jul 22, 2023 pm 04:43 PM

The context package in the Go language is used to pass request context information in the program. It can pass parameters, intercept requests and cancel operations between functions across multiple Goroutines. To use the context package in Go, we first need to import the "context" package. Below is an example that demonstrates how to use the context package to implement request parameter passing. packagemainimport("context&quot

How to solve Vue error: Unable to use props to pass data How to solve Vue error: Unable to use props to pass data Aug 17, 2023 am 10:06 AM

How to solve Vue error: Unable to use props to pass data Preface: During the development process of Vue, it is very common to use props to transfer data between parent and child components. However, sometimes we may encounter a problem, that is, when using props to pass data, an error will occur. This article will focus on how to solve the error that props cannot be used to pass data in Vue. Problem description: In Vue development, when we use props in the parent component to pass data to the child component, if

Syntax and rule analysis of C++ variable parameters Syntax and rule analysis of C++ variable parameters Apr 20, 2024 am 10:15 AM

C++ variable parameters allow functions to accept any number of parameters. The syntax is: returnTypefunctionName(type1arg1,...,typeNargN,...). The rules include: it must be placed after the fixed parameter, there can only be one, the type must be a built-in type, a class object or a pointer, and the quantity is determined when calling. In actual combat, the sum function calculates the sum of all parameters: intsum(intn,...){...}.

Can golang variable parameters be used in generic functions? Can golang variable parameters be used in generic functions? Apr 29, 2024 pm 02:06 PM

In Go, variable parameters can be used for generic functions, allowing the creation of generic functions that accept a variable number of parameters and are suitable for multiple types. For example, you can create a generic function Mode that finds the most frequently occurring element in a given list: Mode accepts a variable number of elements of type T. It counts elements by creating counts for each element. Then it finds the element that appears the most and returns it as mode. In the main function, you can call the Mode function for the list of strings and the list of integers, which will return the string and number with the most occurrences respectively.

See all articles