php变量前加&
引用是什么
在 PHP 中引用意味着用不同的名字访问同一个变量内容。这并不像 C 的指针,它们是符号表别名。注意在 PHP 中,变量名和变量内容是不一样的,因此同样的内容可以有不同的名字。最接近的比喻是 Unix 的文件名和文件本身 - 变量名是目录条目,而变量内容则是文件本身。引用可以被看作是 Unix 文件系统中的紧密连接。
引用做什么
PHP 的引用允许你用两个变量来指向同一个内容。意思是,当你这样做时:
$a =& $b
?>
这意味着 $a 和 $b 指向了同一个变量。
注: $a 和 $b 在这里是完全相同的,这并不是 $a 指向了 $b 或者相反,而是 $a 和 $b 指向了同一个地方。
同样的语法可以用在函数中,它返回引用,以及用在 new 运算符中(PHP 4.0.4 以及以后版本):
$bar =& new fooclass();
$foo =& find_var ($bar);
?>
注: 不用 & 运算符导致对象生成了一个拷贝。如果你在类中用 $this,它将作用于该类当前的实例。没有用 & 的赋值将拷贝这个实例(例如对象)并且 $this 将作用于这个拷贝上,这并不总是想要的结果。由于性能和内存消耗的问题,通常你只想工作在一个实例上面。
尽管你可以用 @ 运算符来关闭构造函数中的任何错误信息,例如用 @new,但用 &new 语句时这不起效果。这是 Zend 引擎的一个限制并且会导致一个解析错误。
引用做的第二件事是用引用传递变量。这是通过在函数内建立一个本地变量并且该变量在呼叫范围内引用了同一个内容来实现的。例如:
function foo (&$var)
{
$var++;
}
$a=5;
foo ($a);
?>
将使 $a 变成 6。这是因为在 foo 函数中变量 $var 指向了和 $a 指向的同一个内容。更多详细解释见引用传递。
引用做的第三件事是引用返回。
引用不是什么
如前所述,引用不是指针。这意味着下面的结构不会产生你预期的效果:
function foo (&$var)
{
$var =& $GLOBALS[ "baz "];
}
foo($bar);
?>
这将使 foo 函数中的 $var 变量在函数调用时和 $bar 绑定在一起,但接着又被重新绑定到了 $GLOBALS[ "baz "] 上面。不可能通过引用机制将 $bar 在函数调用范围内绑定到别的变量上面,因为在函数 foo 中并没有变量 $bar(它被表示为 $var,但是 $var 只有变量内容而没有调用符号表中的名字到值的绑定)。
引用传递
你可以将一个变量通过引用传递给函数,这样该函数就可以修改其参数的值。语法如下:
function foo (&$var)
{
$var++;
}
$a=5;
foo ($a);
// $a is 6 here
?>
注意在函数调用时没有引用符号 - 只有函数定义中有。光是函数定义就足够使参数通过引用来正确传递了。
以下内容可以通过引用传递:
变量,例如 foo($a)
New 语句,例如 foo(new foobar())
从函数中返回的引用,例如:
function &bar()
{
$a = 5;
return $a;
}
foo(bar());
?>
详细解释见引用返回。
任何其它表达式都不能通过引用传递,结果未定义。例如下面引用传递的例子是无效的:
function bar() // Note the missing &
{
$a = 5;
return $a;
}
foo(bar());
foo($a = 5) // 表达式,不是变量
foo(5) // 常量,不是变量
?>
这些条件是 PHP 4.0.4 以及以后版本有的。
引用返回
引用返回用在当你想用函数找到引用应该被绑定在哪一个变量上面时。当返回引用时,使用此语法:
function &find_var ($param)
{
/* ...code... */
return $found_var;
}
$foo =& find_var ($bar);
$foo-> x = 2;
?>
本例中 find_var 函数所返回的对象的属性将被设定(译者:指的是 $foo-> x = 2; 语句),而不是拷贝,就和没有用引用语法一样。
注: 和参数传递不同,这里必须在两个地方都用 & 符号 - 来指出返回的是一个引用,而不是通常的一个拷贝,同样也指出 $foo 是作为引用的绑定,而不是通常的赋值。
取消引用
当你 unset 一个引用,只是断开了变量名和变量内容之间的绑定。这并不意味着变量内容被销毁了。例如:
$a = 1;
$b =& $a;
unset ($a);
?>
不会 unset $b,只是 $a。
再拿这个和 Unix 的 unlink 调用来类比一下可能有助于理解。
引用定位
许多 PHP 的语法结构是通过引用机制实现的,所以上述有关引用绑定的一切也都适用于这些结构。一些结构,例如引用传递和返回,已经在上面提到了。其它使用引用的结构有:
global 引用
当用 global $var 声明一个变量时实际上建立了一个到全局变量的引用。也就是说和这样做是相同的:
$var =& $GLOBALS[ "var "];
?>
这意味着,例如,unset $var 不会 unset 全局变量。
$this
在一个对象的方法中,$this 永远是调用它的对象的引用。

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
