“&"如何PHP中的符号会影响结果吗? [英] How does the '&' symbol in PHP affect the outcome?

查看:83
本文介绍了“&"如何PHP中的符号会影响结果吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了很多PHP函数和变量,并在其中玩过,其中原始作者已经编写了原始代码,而我不得不继续开发产品,即. Joomla组件/模块/插件,我总是想出这个问题:

I've written and played around with alot of PHP function and variables where the original author has written the original code and I've had to continue on developing the product ie. Joomla Components/Modules/Plugins and I've always come up with this question:

&"如何函数或变量上附加的符号会影响结果吗?

How does the '&' symbol attached to a function or a variable affect the outcome?

例如:

$variable1 =& $variable2;

OR

function &usethisfunction() {
}

OR

function usethisfunction(&thisvariable) {
{

我尝试搜索PHP手册和其他相关资源,但找不到任何能解决我的问题的东西.

I've tried searching through the PHP manual and other related sources but cannot find anything that specifically addresses my question.

推荐答案

这些被称为以下是一些常规" PHP代码的示例:

Here is an example of some "regular" PHP code:

function alterMe($var) {
    $var = 'hello';
}

$test = 'hi';
alterMe($test);
print $test; // prints hi

$a = 'hi';
$b = $a;
$a = 'hello';
print $b; // prints hi

这是使用引用可以实现的目标:

And this is what you can achieve using references:

function alterMe(&$var) {
    $var = 'hello';
}

$test = 'hi';
alterMe($test);
print $test; // prints hello

$a = 'hi';
$b &= $a;
$a = 'hello';
print $b; // prints hello

具体细节在文档中.本质上,但是:

The nitty gritty details are in the documentation. Essentially, however:

PHP中的引用是通过不同名称访问相同变量内容的一种方式.它们不像C指针.相反,它们是符号表别名.请注意,在PHP中,变量名称和变量内容不同,因此相同的内容可以具有不同的名称.最接近的类比是Unix的文件名和文件-变量名是目录项,而变量内容是文件本身.引用可以比喻为Unix文件系统中的硬链接.

References in PHP are a means to access the same variable content by different names. They are not like C pointers; instead, they are symbol table aliases. Note that in PHP, variable name and variable content are different, so the same content can have different names. The closest analogy is with Unix filenames and files - variable names are directory entries, while variable content is the file itself. References can be likened to hardlinking in Unix filesystem.

这篇关于“&"如何PHP中的符号会影响结果吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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