关于php的两个符号@和$---php总会要知道的系列

<article>
        <div id="article_content" class="article_content clearfix csdn-tracking-statistics" data-pid="blog" data-mod="popu_307" data-dsm="post">
                                            <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-bb1edad192.css">
                        <div class="htmledit_views">
                
<p>在写代码的时候,碰到了在函数和变量前家 @和$的的问题,于是就借这个机会,学习下php的<span style="color:rgb(51,51,51);font-family:Arial, Helvetica, simsun, u5b8bu4f53;font-size:14px;line-height:25px;">传值和传引用这两种方式</span></p>
<p><br></p>
<p><br></p>
<p>首先</p>
<p><span style="font-family:arial, '宋体', sans-serif;font-size:14px;line-height:24px;background-color:rgb(255,252,246);"></span></p>
<pre id="best-answer-content" class="reply-text mb10" style="font-family:Arial;line-height:22px;" name="code">@ 运算符只对表达式有效。对新手来说一个简单的规则就是:如果能从某处得到值,就能在它前面加上 @ 运算符。例如,可以把它放在变量,函数和 include() 调用,常量,等等之前。不能把它放在函数或类的定义之前,也不能用于条件结构例如 if 和 foreach 等。

目前的“@”错误控制运算符前缀甚至使导致脚本终止的严重错误的错误报告也失效。这意味着如果在某个不存在或类型错误的函数调用前用了“@”来抑制错误信息,那脚本会没有任何迹象显示原因而死在那里。</pre>
一句话,即 抑制报错的一种手段
<p></p>
<p><br></p>
<p><br></p>
<p>对于$ 则是我们的重点问题</p>
<p><br></p>
<p><span style="color:rgb(51,51,51);font-family:Arial, Helvetica, simsun, u5b8bu4f53;font-size:14px;line-height:25px;">PHP代码:<br style="line-height:25px;">
基本形式:<br style="line-height:25px;"><span style="line-height:25px;"><span style="color:#3366cc;line-height:1.3em;">function&nbsp;&amp;find_var($param)&nbsp;//这里用&amp;在对象前面,作用为声明<br style="line-height:25px;">
{<br style="line-height:25px;">
&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;...code...&nbsp;*/<br style="line-height:25px;">
&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$found_var;<br style="line-height:25px;">
}</span></span><br style="line-height:25px;"><span style="line-height:25px;"><span style="color:#3366cc;line-height:1.3em;">$foo&nbsp;=&amp;&nbsp;find_var($bar);&nbsp;//这里用&amp;,作用为联系变量</span></span><br style="line-height:25px;"><span style="line-height:25px;"><span style="color:#3366cc;line-height:1.3em;">$foo-&gt;x&nbsp;=&nbsp;2;&nbsp;//赋值给函数<br style="line-height:25px;"></span></span>&nbsp;<br style="line-height:25px;"><span style="color:#000000;line-height:1.3em;">例子:<br style="line-height:25px;"></span>function&nbsp;&amp;test()&nbsp;&nbsp;&nbsp;<br style="line-height:25px;">
{&nbsp;&nbsp;&nbsp;<br style="line-height:25px;">
static&nbsp;$b=0;//申明一个静态变量&nbsp;&nbsp;&nbsp;<br style="line-height:25px;">
$b=$b+1;&nbsp;&nbsp;&nbsp;<br style="line-height:25px;">
echo&nbsp;$b;&nbsp;&nbsp;&nbsp;<br style="line-height:25px;">
return&nbsp;$b;&nbsp;&nbsp;&nbsp;<br style="line-height:25px;">
}&nbsp;&nbsp;&nbsp;<br style="line-height:25px;">
$a=test();//这条语句会输出$b的值为1,<br style="line-height:25px;">
$a=5;&nbsp;&nbsp;&nbsp;<br style="line-height:25px;">
$a=test();//这条语句会输出$b的值为2&nbsp;,因为没有引用返回,$a=5,不对对象起赋值作用<br style="line-height:25px;">
$a=&amp;test();//这条语句会输出$b的值为3&nbsp;,成功引用返回,下面的代码起作用<br style="line-height:25px;">
$a=5;&nbsp;&nbsp;&nbsp;<br style="line-height:25px;">
$a=test();//这条语句会输出$b的值为6&nbsp;&nbsp;<br style="line-height:25px;">
下面解释下:&nbsp;<br style="line-height:25px;">
通过这种方式$a=test();得到的其实不是函数的引用返回,这跟普通的函数调用没有区别。至于原因:这是PHP的规定,<br style="line-height:25px;">
php规定通过$a=&amp;test();&nbsp;方式得到的才是函数的引用返回。<br style="line-height:25px;">
至于什么是引用返回呢(PHP手册上说:引用返回用在当想用函数找到引用应该被绑定在哪一个变量上面时。)&nbsp;<br style="line-height:25px;">
这句狗屁话害我半天没看懂。<br style="line-height:25px;">
用上面的例子来解释就是<br style="line-height:25px;">
$a=test()方式调用函数,只是将函数的值赋给$a而已,而$a做任何改变都不会影响到函数中的$b,通过$a=&amp;test()方式调用函数呢,&nbsp;他的作用是将return&nbsp;$b中的$b变量的内存地址与$a变量的内存地址,向了同一个地方,产生了相当于这样的效果($a=&amp;b;)&nbsp;所以改变$a的值,也同时改变了$b的值,所以在执行了:$a=&amp;test();<br style="line-height:25px;">
$a=5;<br style="line-height:25px;">
以后,$b的值变为了5&nbsp;......<br style="line-height:25px;"><br style="line-height:25px;">
php函数前面加&amp;符号是什么意思?有的函数前面会加&amp;,因为很少见到,所以不明白php函数前面加&amp;符号是什么意思<br style="line-height:25px;">
那么,php函数前面加&amp;符号有什么作用呢?&nbsp;&nbsp;<br style="line-height:25px;"><br style="line-height:25px;">
Java代码&nbsp;<br style="line-height:25px;">
function&nbsp;&amp;test()&nbsp;&nbsp;&nbsp;<br style="line-height:25px;">
{&nbsp;&nbsp;&nbsp;<br style="line-height:25px;">
static&nbsp;$b=0;//申明一个静态变量&nbsp;&nbsp;&nbsp;<br style="line-height:25px;">
$b=$b+1;&nbsp;&nbsp;&nbsp;<br style="line-height:25px;">
echo&nbsp;$b;&nbsp;&nbsp;&nbsp;<br style="line-height:25px;">
return&nbsp;$b;&nbsp;&nbsp;&nbsp;<br style="line-height:25px;">
}&nbsp;&nbsp;&nbsp;<br style="line-height:25px;">
&nbsp;&nbsp;<br style="line-height:25px;">
$a=&amp;test();//这条语句会输出&nbsp;$b的值&nbsp;为1&nbsp;&nbsp;&nbsp;<br style="line-height:25px;">
$a=6;&nbsp;&nbsp;&nbsp;<br style="line-height:25px;">
test();//这条语句会输出$b的值&nbsp;为7&nbsp;<br style="line-height:25px;">
&nbsp;<br style="line-height:25px;">
php变量前面加&amp;符号是什么意思<br style="line-height:25px;"><br style="line-height:25px;"><br style="line-height:25px;">
先看一个示例<br style="line-height:25px;"><br style="line-height:25px;"><br style="line-height:25px;">
Php代码&nbsp;<br style="line-height:25px;"><br style="line-height:25px;">
&nbsp;<br style="line-height:25px;">
$foo&nbsp;=&nbsp;321;<br style="line-height:25px;">
$bar&nbsp;=&nbsp;&amp;$foo;&nbsp;<br style="line-height:25px;">
$bar&nbsp;=&nbsp;123;<br style="line-height:25px;">
print&nbsp;$foo;那么输出的结果将会是什么呢<br style="line-height:25px;"><br style="line-height:25px;"><br style="line-height:25px;">
Php代码&nbsp;<br style="line-height:25px;">
123&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height:25px;">
&nbsp;&nbsp;<br style="line-height:25px;">
为什么会这样呢?<br style="line-height:25px;"><br style="line-height:25px;"><br style="line-height:25px;"><span style="line-height:25px;"><span style="color:#3366cc;line-height:1.3em;">改动新的变量将影响到原始变量,这种赋值操作更加快速。</span></span><br style="line-height:25px;">
注意:只有命名变量才可以传地址赋值,就是说,改变了$bar的值,也就改变了$foo的值了。<br style="line-height:25px;">
&nbsp;<br style="line-height:25px;">
另一个例子:<br style="line-height:25px;"><br style="line-height:25px;">
$_GET[1]&nbsp;=&nbsp;1;<br style="line-height:25px;">
function&nbsp;&amp;a()<br style="line-height:25px;">
{<br style="line-height:25px;">
&nbsp;$a&nbsp;=&nbsp;$_GET[1];<br style="line-height:25px;">
&nbsp;return&nbsp;$a;<br style="line-height:25px;">
}<br style="line-height:25px;">
$x&nbsp;=&amp;&nbsp;a();<br style="line-height:25px;">
$x&nbsp;=&nbsp;'MoontoC';<br style="line-height:25px;">
echo&nbsp;$_GET[1];&nbsp;//&nbsp;此时这里会显示&nbsp;MoontoC,&nbsp;而不是最初赋值的1,&nbsp;你明白其中的意义了吗,&nbsp;使用函数传值时必须双方都使用引用符号才有意义,才能真正引用,&nbsp;而任何一边少了引用符号,&nbsp;都不会得到错误的内容,&nbsp;但内容是传值,&nbsp;而不是传引用了。没有程序基础的人最初学php的确很难理解传值和传引用的重要性,&nbsp;觉得反正都能得到自己要的东西,&nbsp;其实不是的,&nbsp;很多时候虽然得到的东西一样,&nbsp;但是代价却完全不一样,&nbsp;一个长达200万字的值,&nbsp;被当作值传送就是400万字同时放在内存中待用,&nbsp;多耗费一倍内存的意思,而传引用就只是一个快捷方式传送过去而已。</span><br></p>
<p><span style="color:rgb(51,51,51);font-family:Arial, Helvetica, simsun, u5b8bu4f53;font-size:14px;line-height:25px;"><br></span></p>
<p><span style="color:rgb(51,51,51);font-family:Arial, Helvetica, simsun, u5b8bu4f53;font-size:14px;line-height:25px;"><br></span></p>
<p><span style="font-family:Arial, Helvetica, simsun, u5b8bu4f53;color:#333333;"><span style="font-size:14px;line-height:25px;">解释2:</span></span></p>
<p><span style="font-family:Arial, Helvetica, simsun, u5b8bu4f53;color:#333333;"><span style="font-size:14px;line-height:25px;"><br></span></span></p>
<p><span style="font-family:Arial, Helvetica, simsun, u5b8bu4f53;color:#333333;"><span style="font-size:14px;line-height:25px;"><span style="color:rgb(51,51,51);font-family:Arial;font-size:14px;line-height:26px;"></span></span></span></p>
<p>就是在变量或者函数、对象等前面加上&amp;符号</p>
<br>
在PHP 中引用的意思是:不同的名字访问同一个变量内容.<br>
与C语言中的指针是有差别的.C语言中的指针里面存储的是变量的内容在内存中存放的地址<br><br>
变量的引用<br><br>
PHP 的引用允许你用两个变量来指向同一个内容<br>
[php]&nbsp;<br>
&lt;?<br>
$a="ABC";<br>
$b =&amp;$a;<br>
echo $a;//这里输出:ABC<br>
echo $b;//这里输出:ABC<br>
$b="EFG";<br>
echo $a;//这里$a的值变为EFG 所以输出EFG<br>
echo $b;//这里输出EFG<br>
?&gt;<br>
[/php]<br><br>
函数的传址调用<br>
传址调用我就不多说了 下面直接给出代码<br>
[php]<br>
function test(&amp;$a)<br>
{<br>
$a=$a+100;<br>
}<br>
$b=1;<br>
echo $b;//输出1<br>
test($b);&nbsp;&nbsp; //这里$b传递给函数的其实是$b的变量内容所处的内存地址,通过在函数里改变$a的值 就可以改变$b的值了<br>
echo "&lt;br&gt;";<br>
echo $b;//输出101<br>
[/php]<br>
要注意的是,在这里test(1);的话就会出错,原因自己去想<br><br>
函数的引用返回<br>
先看代码<br>
[php]<br>
function &amp;test()<br>
{<br>
static $b=0;//申明一个静态变量<br>
$b=$b+1;<br>
echo $b;<br>
return $b;<br>
}<br><br>
$a=test();//这条语句会输出 $b的值 为1<br>
$a=5;<br>
$a=test();//这条语句会输出 $b的值 为2<br><br>
$a=&amp;test();//这条语句会输出 $b的值 为3<br>
$a=5;<br>
$a=test();//这条语句会输出 $b的值 为6<br>
[/php]<br>
下面解释下: <br>
通过这种方式$a=test();得到的其实不是函数的引用返回,这跟普通的函数调用没有区别 至于原因: 这是PHP的规定<br>
PHP规定通过$a=&amp;test(); 方式得到的才是函数的引用返回<br>
至于什么是引用返回呢(PHP手册上说:引用返回用在当想用函数找到引用应该被绑定在哪一个变量上面时。) 这句狗屁话 害我半天没看懂<br><br>
用上面的例子来解释就是<br>
$a=test()方式调用函数,只是将函数的值赋给$a而已, 而$a做任何改变 都不会影响到函数中的$b<br>
而通过$a=&amp;test()方式调用函数呢, 他的作用是 将return $b中的 $b变量的内存地址与$a变量的内存地址 指向了同一个地方<br>
即产生了相当于这样的效果($a=&amp;b;) 所以改变$a的值 也同时改变了$b的值 所以在执行了<br>
$a=&amp;test();<br>
$a=5;<br>
以后,$b的值变为了5<br><br>
这里是为了让大家理解函数的引用返回才使用静态变量的,其实函数的引用返回多用在对象中<br><br>
对象的引用<br>
[php]<br>
&lt;?<br>
class a{<br>
var $abc="ABC";<br>
}<br>
$b=new a;<br>
$c=$b;<br>
echo $b-&gt;abc;//这里输出ABC<br>
echo $c-&gt;abc;//这里输出ABC<br>
$b-&gt;abc="DEF";<br>
echo $c-&gt;abc;//这里输出DEF<br>
?&gt;<br>
[/php]<br>
以上代码是在PHP5中的运行效果<br>
在PHP5中 对象的复制 是通过引用来实现的。上列中$b=new a; $c=$b; 其实等效于$b=new a; $c=&amp;$b;<br>
PHP5中默认就是通过引用来调用对象, 但有时你可能想建立一个对象的副本,并希望原来的对象的改变不影响到副本 . 为了这样的目的,PHP定义了一个特殊的方法,称为__clone.<br><br>
引用的作用<br>
如果程序比较大,引用同一个对象的变量比较多,并且希望用完该对象后手工清除它,个人建议用 "&amp;" 方式,然后用$var=null的方式清除. 其它时候还是用php5的默认方式吧. 另外, php5中对于大数组的传递,建议用 "&amp;" 方式, 毕竟节省内存空间使用。<br><br><br>
取消引用<br>
当你 unset 一个引用,只是断开了变量名和变量内容之间的绑定。这并不意味着变量内容被销毁了。例如:&nbsp;<br><br>
&lt;?php<br>
$a = 1;<br>
$b =&amp; $a;<br>
unset ($a);<br>
?&gt;&nbsp;&nbsp;<br><br>
不会 unset $b,只是 $a。&nbsp;<br><br><br>
global 引用<br>
当用 global $var 声明一个变量时实际上建立了一个到全局变量的引用。也就是说和这样做是相同的:&nbsp;<br><br>
&lt;?php<br>
$var =&amp; $GLOBALS["var"];<br>
?&gt;&nbsp;&nbsp;<br><br>
这意味着,例如,unset $var 不会 unset 全局变量。&nbsp;<br><br>
$this<br>
在一个对象的方法中,$this 永远是调用它的对象的引用。<br><br><br>
//下面再来个小插曲<br>
php中对于地址的指向(类似指针)功能不是由用户自己来实现的,是由Zend核心实现的,php中引用采用的是“写时拷贝”的原理,就是除非发生写操作,指向同一个地址的变量或者对象是不会被拷贝的。<br><br>
通俗的讲<br>
1:如果有下面的代码<br>
[php]&nbsp;<br>
$a="ABC";<br>
$b=$a;<br>
[/php]<br>
其实此时 $a与$b都是指向同一内存地址 而并不是$a与$b占用不同的内存<br><br>
2:如果在上面的代码基础上再加上如下代码<br>
[php]&nbsp;<br>
$a="EFG";<br>
[/php]<br>
由于$a与$b所指向的内存的数据要重新写一次了,此时Zend核心会自动判断 自动为$b生产一个$a的数据拷贝,重新申请一块内存进行存储<br><p><br></p>
            </div>
                </div>
                                    
                    <script>
                        (function(){
                            function setArticleH(btnReadmore,posi){
                                var winH = $(window).height();
                                var articleBox = $("div.article_content");
                                var artH = articleBox.height();
                                if(artH > winH*posi){
                                    articleBox.css({
                                        'height':winH*posi+'px',
                                        'overflow':'hidden'
                                    })
                                    btnReadmore.click(function(){
                                        articleBox.removeAttr("style");
                                        $(this).parent().remove();
                                    })
                                }else{
                                    btnReadmore.parent().remove();
                                }
                            }
                            var btnReadmore = $("#btn-readmore");
                            if(btnReadmore.length>0){
                                if(currentUserName){
                                    setArticleH(btnReadmore,3);
                                }else{
                                    setArticleH(btnReadmore,1.2);
                                }
                            }
                        })()
                    </script>
                    </article>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值