Rumah  >  Artikel  >  pembangunan bahagian belakang  >  php变量引用赋值一些知识

php变量引用赋值一些知识

WBOY
WBOYasal
2016-07-28 08:26:121267semak imbas

手册上的解释是:简单的引用(换言之,“成为其别名” 或者 “指向”)了原始变量。改动新的变量将影响到原始变量,反之亦然。
我理解决引用相当一个人取了两个名字,无论叫个名字都是这个人。
以下两例子结果都在一样的。删除或修改都会影响到另外一个变量的值。

<code><?php <span>$foo = <span>'Bob'</span>;              <span>//</span> 将 <span>'Bob'</span> 赋给 <span>$foo</span><span>$bar</span> = &<span>$foo</span>;              <span>//</span> 通过 <span>$bar</span> 引用 <span>$foo</span><span>$bar</span> = <span>"My name is $bar"</span>;  <span>//</span> 修改 <span>$bar</span> 变量
echo <span>$bar</span>;
echo <span>$foo</span>;                 <span>//</span><span>$foo</span> 的值也被修改
?>


<?php <span>$foo = <span>'Bob'</span>;              <span>//</span> 将 <span>'Bob'</span> 赋给 <span>$foo</span><span>$bar</span> = &<span>$foo</span>;              <span>//</span> 通过 <span>$bar</span> 引用 <span>$foo</span><span>$foo</span> = <span>"My name is $bar"</span>;  <span>//</span> 修改 <span>$foo</span> 变量
echo <span>$bar</span>,<span>"\n"</span>;
echo <span>$foo</span>;                 <span>//</span><span>$foo</span> 的值也被修改
?></code>

删除引用时,无论是删除ba结果都没会影响到另一个变量。只是相当于删除一个名称,不会影响到变量本身。

<code><?php <span>$a = <span>0</span>;
<span>$b</span> = &<span>$a</span>;
  //  <span>echo</span><span>$a</span>; //<span>0</span>
   // <span>unset</span>(<span>$b</span>); // <span>unset</span><span>$b</span><span>unset</span>(<span>$a</span>); // <span>unset</span><span>$b</span><span>echo</span><span>$b</span>; //<span>0</span> it<span>'s OK</span></code>

当一个变量先后引用两个变量时,把第一次引用的变量,修改成第二次引用的变量。第一次引用没有关系,互相不会影响。

<code><span><span><?php </span><span>$a</span> = <span>"hihaha"</span>;
<span>$b</span> = &<span>$a</span>;
<span>$c</span> = <span>"eita"</span>;
<span>$b</span> = &<span>$c</span>;
<span>echo</span><span>$b</span>; <span>// shows "hihaha"</span></span></span></code>

函数的传址调用传址调用,执行完,相当于直接改变引用的变量。这里bb的变量内容所处的内存地址,通过在函数里改变ab的值了

<code><span><span>function</span><span>test</span><span>( &<span>$a</span> )</span>
{</span><span>$a</span> += <span>10</span>;
  <span>return</span><span>$a</span>;
}
<span>$b</span> = <span>1</span>;

test(<span>$b</span>);

<span>echo</span><span>$b</span>;<span>//输出11</span></code>

函数的引用返回

<code>function &test2()
{
    <span>$b</span>  = <span>0</span>;<span>//</span>申明一个静态变量
    <span>$b</span>  =   <span>$b</span>+<span>1</span>;
    <span>return</span><span>$b</span>;
}

<span>$a</span> = test2();<span>//</span>这条语句会输出 <span>$b</span>的值 为1
var_dump(<span>$a</span>);

<span>$a</span> = test2();<span>//</span>这条语句会输出 <span>$b</span>的值 为<span>2</span>
var_dump(<span>$a</span>);
<span>$a</span> = & test2();<span>//</span>这条语句会输出 <span>$b</span>的值 为<span>3</span>
var_dump(<span>$a</span>);
<span>$a</span> = <span>5</span>;
<span>$a</span> = test2();<span>//</span>这条语句会输出 <span>$b</span>的值 为<span>6</span>
var_dump(<span>$a</span>);</code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了 php变量引用赋值一些知识,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:PHP如何生成验证码Artikel seterusnya:性能优化大全