PHP:&是什么?前面的变量名是什么意思? [英] PHP: What does a & in front of a variable name mean?

查看:64
本文介绍了PHP:&是什么?前面的变量名是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What does a & in front of a variable name mean?

例如& $ salary与$ salary

For example &$salary vs. $salary

推荐答案

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

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'
?>

这篇关于PHP:&amp;是什么?前面的变量名是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆