Home > Backend Development > PHP Tutorial > How to understand the ampersand here?

How to understand the ampersand here?

WBOY
Release: 2016-08-04 09:20:31
Original
1004 people have browsed it

<code>    /**
     * 获取各请求方法PV数
     * 
     * @param integer $interval 周期间隔
     * @param string $referenceTime 基准时间
     * @param array $domains 域名列表
     * @param array $paths 路径列表
     * @param array $methods 请求方法列表
     * @param string $pvCountOrder PV数排序 - 正序:ASC 倒序:DESC
     * @param integer $offset 结果偏移
     * @param integer $limit 结果数量
     * @param boolean &$total 结果总数
     * @return array PV数列表
     */
    public function getPvCountForMethods(
        $interval,
        $referenceTime,
        $domains = array(),
        $paths = array(),
        $methods = array(),
        $pvCountOrder = 'DESC',
        $offset = 0,
        $limit = -1,
        &$total = false    
    ) {
        // 确定请求方法PV日志表
        $logTable = $paths</code>
Copy after login
Copy after login

Help: How to understand the & symbol in the above php parameters?

Reply content:

<code>    /**
     * 获取各请求方法PV数
     * 
     * @param integer $interval 周期间隔
     * @param string $referenceTime 基准时间
     * @param array $domains 域名列表
     * @param array $paths 路径列表
     * @param array $methods 请求方法列表
     * @param string $pvCountOrder PV数排序 - 正序:ASC 倒序:DESC
     * @param integer $offset 结果偏移
     * @param integer $limit 结果数量
     * @param boolean &$total 结果总数
     * @return array PV数列表
     */
    public function getPvCountForMethods(
        $interval,
        $referenceTime,
        $domains = array(),
        $paths = array(),
        $methods = array(),
        $pvCountOrder = 'DESC',
        $offset = 0,
        $limit = -1,
        &$total = false    
    ) {
        // 确定请求方法PV日志表
        $logTable = $paths</code>
Copy after login
Copy after login

Help: How to understand the & symbol in the above php parameters?

& represents a reference to a variable in PHP.

Putting it in this code means that the pointer inside the parameter $total points to the place passed.
The PHP interpreter will not register a new variable $total in the body of this function, but directly refers to the place passed The $total variable,
If the value of $total is modified within the function body, the $total outside will also change accordingly
example:

<code><?php
$a = 1;

function foo(&$var)
{
    return $var = $var + 10;
}

echo foo($a);//11
echo $a; //11


?></code>
Copy after login

There are two simple types of transfer
One is value transfer
The other is reference transfer
&It is reference transfer

Should you pass by value or by reference? &$total means passing the address of $total

Look at this: This is a manual
Then, git blame filename, find the author, and beat him to death.

Supports blame killing, 9 parameters...

Reference transfer can be regarded as a pointer, pointing to the same memory address

Quote
Function:

<code>修改同一个值,因为内存地址是一样的</code>
Copy after login
Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template