且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP:&是什么?前面的变量名是什么意思?

更新时间:2023-02-26 10:28:11

它将变量的引用传递给变量,因此当编辑分配了该引用的任何变量时,原始变量将被更改.在制作更新现有变量的函数时,它们确实很有用.无需对要更新的变量进行硬编码,您只需将引用传递给该函数即可.

It passes a reference to the variable so when any variable assigned the reference is edited, the original variable is changed. They are really useful when making functions which update an existing variable. Instead of hard coding which variable is updated, you can simply pass a reference to the function instead.

示例

<?php
    $number = 3;
    $pointer = &$number;  // Sets $pointer to a reference to $number
    echo $number."<br/>"; // Outputs  '3' and a line break
    $pointer = 24;        // Sets $number to 24
    echo $number;         // Outputs '24'
?>