PHP在函数体中传递与接收参数
在PHP的函数中,参数传递可以分为值传递和引用传递(也称为地址传递)两种。
默认情况下,PHP是按值传递参数的。值传递参数调用函数时将常量或变量的值(通常称其为实参)传递给函数的参数(通常称为形参)。值传递的特点是实参与行参分别存储在内存中,是两个不相关的独立变量。因此,在函数内部改变形参的值时,实参的值一般是不会改变的。
引用传递(按地址传递)的特点是实参与行参共享一块内存。因此,当形参的值改变的时候,实参的值也会相应的做出改变。从这种角度上说,可以认为形参合实参是同一个变量。
定义引用传递参数时,可以在参数前面加上引用符号&。
<?php function printString(&$string){ echo($string); $string="打印完成"; } $str="测试字符!n"; printString($str); echo($str); ?> // 程序输出:测试字符! 打印完成
php还支持可变长度的参数列表。在定义函数时,不指定参数。在调用函数时,可以根据需要指定参数的数量,通过与参数相关的几个系统函数获取参数信息。具体说明为:
<?php function mysum() { $num = func_num_args(); echo("函数包含:".$num."个参数n"); $sum = 0; for($i=0; $i < $num; $i++) { $sum = $sum + func_get_arg($i); } echo("参数累加纸盒为:".$sum); } mysum(1,2,3,4); ?> // 程序输出:函数包含:4个参数 参数累加纸盒为:10
func_num_args函数功能:返回传递到函数的参数数目,其语法如下 : int func_num_args (void )。说明 : 返回传递到目前定义函数的参数数目。如果是从函数定义的外面来呼叫此函数,则func_get_arg( )将会产生警告。
func_num_args( )可以用来结合func_get_arg( )和func_get_args( )来允许使用者定义的函式接受variable-length参数列表。其中,func_get_arg( )从参数列表返回项目,其语法:int func_get_arg (int arg_num),传回定义函数的参数列表的第arg_num个参数,其参数从0开始。且函数定义的外面来呼叫此函数会产生警告;并且当arg_num大于函数实际传递的参数数目时亦会产生警告并返回FALSE。
func_get_args()函数和func_get_arg()函数的区别在于,func_get_args()函数传回一数组,数组的各个元素相当于是目前使用者定义函式的参数列的数目。
在我们构建PHP类的时候,灵活使用这三个函数,可以起到非常理想的效果,例如外面在创建PHP和MYSQL链接的类时,可以书写如下代码:
<?php class mydb { private $user; private $pass; private $host; private $db; public function __construct(){ $num_args=func_num_args(); if($num_args>0){ $args=func_get_args(); $this->host=$args[0]; $this->user=$args[1]; $this->pass=$args[2]; $this->connect(); } } } ?>
再给出一个示例程序:
<?php function foo() { $numargs = func_num_args(); echo "Number of arguments: $numargs<br>n"; if ($numargs >= 2) { echo "Second argument is: " . func_get_arg (1) . "<br>n"; } $arg_list = func_get_args(); for ($i = 0; $i < $numargs; $i++) { echo "Argument $i is: " . $arg_list[$i] . "<br>n"; } } foo (1, 2, 3); ?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
