最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

PHP引用(&)案例介绍

来源:动视网 责编:小采 时间:2020-11-03 18:17:48
文档

PHP引用(&)案例介绍

PHP引用(&)案例介绍:变量引用案例:<php //变量单独引用 $test = "a string!\n"; $quote = &$test; echo $test; //a string! echo $quote;//a string! $quote = "exchange!\n"; echo $test;
推荐度:
导读PHP引用(&)案例介绍:变量引用案例:<php //变量单独引用 $test = "a string!\n"; $quote = &$test; echo $test; //a string! echo $quote;//a string! $quote = "exchange!\n"; echo $test;
 变量引用

案例:

<?php
//变量单独引用
$test = "a string!
";
$quote = &$test;

echo $test; //a string!
echo $quote;//a string!

$quote = "exchange!
";
echo $test; //exchange!
echo $quote;//exchange!

unset($quote);
echo $test; //exchange!

//函数传参引用
$num = 10;
echo $num."
";
test($num);
echo $num."
";

function test( &$num) {
 $num += 10;
}

结论

PHP 的变量引用相当于不同的变量都指向同一个内容。也会操作这一份内容,如果删除其中一个引用,另外一个引用不受影响,相当与删除的变量不在指向同一个内容,但是不能为常量。(相当于一个人有好多个名字,及时去掉一个名字,也不会影响人这个实体)

推荐:php培训

函数引用返回

案例1:

<?php

$t = test(); // 1
var_dump($t); // int(1)
$t = 20; $t = test(); //2
var_dump($t); //int(2)
$t = &test(); //3
var_dump($t) ; //int(3)
$t = 20; $t = test(); //21
var_dump($t); //int(21)

function &test() {
 static $test = 0;
 $test += 1;
 echo $test."
";
 
 return $test; 
}

案例2:

?php

$t = test();

function &test() {
 static $test = 0;
 $test += 1;
 echo $test."
";
}


$ php test.php 
1
PHP Notice: Only variable references should be returned by reference in /home/q/www/zt.dujia.qunar.com/test.php on line 9

结论:

相当于 $t = &test; 说白了还是 变量之间的引用。

对象引用

案例:

<?php
class a{
 var $abc="ABC";
} 
$b=new a; 
$c=$b; 
echo $b->abc;//这里输出ABC 
echo $c->abc;//这里输出ABC
 $b->abc="DEF"; 
echo $c->abc;//这里输出DEF
?>

引用,其实就是一个变量空间,赋予了多个操作名称,这些操作名称,都指向同一个操作空间,其中一个操作名称对其空间操作,其他操作名称得到的结果也会变化。

引用的作用:

一般来说,每次声明一个变量,其就会占用一定的空间。

函数传参的方便,直接修改原有空间的内容,同时也减少中间变量的空间的开销。

更多php相关知识请关注php免费培训网站。

文档

PHP引用(&)案例介绍

PHP引用(&)案例介绍:变量引用案例:<php //变量单独引用 $test = "a string!\n"; $quote = &$test; echo $test; //a string! echo $quote;//a string! $quote = "exchange!\n"; echo $test;
推荐度:
标签: 介绍 php 案例
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top