PHP function reference parameter function
Introduction to functions that reference parameters
In PHP, parameters are passed by value by default, and the parameters of the function are also local variables, so even in the function Changing the value of a parameter internally does not change the value outside the function. The previous section introduced the regular parameter functions and pseudo-type parameter functions in php. This section will talk about how to reference parameters. When the function is a subprogram, the program that calls the function can be called the parent program. The parent program directly passes the specified value or variable to the function for use. Since the passed value or variable and the value in the function are stored in different memory blocks, if the function makes any changes to the imported value, it will not directly affect the parent program.
The function format description of php reference parameters is as follows:
void funName(array &arg) // 在参数列表中出现使用 &描述的参数
The following is an example of php reference parameters:
<?php //声明一个函数作为测试 function test($a){ $a = 200; // 函数中改变参数值为200 } $b = 100; // 父程序中声明一个全局变量$b并给一个初始值100 test($b); // 调用test函数 将100传给函数的参数 echo $b; // 输出100 $b的值没变化 ?>
In In the above example, when calling the test() function, the value of the global variable $b is passed to the function test(). Although the variable $a is assigned a new value of 200 in the test() function, the value of the variable $b outside the function cannot be changed. After calling the test() function, the value output by the variable $b is still 100. If you want to allow a function to modify its argument values, you must pass the argument by reference.
Compared to the pass-by-value mode, the specified value or target variable in the parent program is not passed to the function, but the relative address of the memory storage block of the value or variable is imported into the function. Therefore, when the value is changed in any way in the function, it will also affect the parent program. If you want a parameter of a function to always be passed by reference, prepend the symbol & in the function definition.
Modify the previous example:
<?php //声明一个函数作为测试 function test(&$a){ $a = 200; // 函数中改变参数值为200,加使用 & 引用参数$a,外部变量被修改 } $b = 100; // 父程序中声明一个全局变量$b并给一个初始值100 test($b); // 调用test函数 将变量$b的引用传给函数的参数$a echo $b; // 输出200 $b的值在函数中修改变量$a时被修改 ?>
In the above example, when calling the test() function, the value of the global variable $b is not passed to the function. test(). As you can see, in the definition of the test() function, the reference symbol & is used to specify that the variable $a is passed by reference. In the function body, the variable $a is assigned a new value of 200. Since the external data will be modified by reference, the value of the external variable $b is also modified. After the function call ends, you can see that the output value of variable $b is 200.
Note: If you use & to modify the parameters in the formal parameters of the function. Then when calling the function, you must pass in a variable to this parameter, but not a value.
【Recommended related tutorials】
1. "php.cn Dugu Jiujian (4)-php video tutorial"
2. php programming from entry to master a full set of video tutorials
3. php practical video tutorial
The above is the detailed content of PHP function reference parameter function. For more information, please follow other related articles on the PHP Chinese website!

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

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

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

Validator can be created by adding the following two lines in the controller.
