Home >Backend Development >PHP Tutorial > php5 值赋值与引述赋值

php5 值赋值与引述赋值

WBOY
WBOYOriginal
2016-06-13 13:03:36912browse

php5 值赋值与引用赋值
值赋值――2个变量有各自的内存,互不影响

$str1 = "wang";
$str2 = $str1;
$str1 = "zhang";
echo $str2; // wang


引用赋值――用“&”,2个变量共用一个内存,一个变化,另一个也变化。

$i = "zhao";
$j = "wang";
$j = &$i;

echo $j; // zhao

$j = "hello,$j";
echo $j; //hello,zhao
echo "<br/>";
echo $i; //hello,zhao


需要注意的是只有命名变量才可以传地址赋值,这一点非常重要。

<?php
$foo = 25;
$bar = &$foo;      // This is a valid assignment.
$bar = &(24 * 7);  // Invalid; references an unnamed expression.

function test()
{
   return 25;
}

$bar = &test();    // Invalid.
?>  


Statement:
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