Applications referenced in php_PHP tutorial

WBOY
Release: 2016-07-13 10:05:45
Original
812 people have browsed it

Apps referenced in php

<?php 

// http://blog.csdn.net/samxx8/article/details/37564103
/**
在PHP 中引用的意思是:不同的名字访问同一个变量内容.
(1) 变量之间的引用: 

$a = 10 ; $b =$a ;  // 此时$b和$a所指向的是同一存储地址 ,如果$a = 11 ; 则zend会开辟一个新的单元从来是的$a = 11 ; $b 还是原来的地址

$a = 10 ; $b =&$a ; // 此时$b 就是$a的别名,比如这个人叫小张,大名叫张三一样,如果 $a = 11 ; $b 也会跟着变 

*/
 
 
$a="ABC"; 
$b =&$a; 
echo $a;//这里输出:ABC 
echo $b;//这里输出:ABC 
$b="EFG"; 
echo $a;//这里$a的值变为EFG 所以输出EFG echo $b;//这里输出EFG 

// (2) 函数的传址调用(这里使用引用的意义在于,对于大数组节省内存空间)

function conTest(&$p_vale){
	 $p_vale = $p_vale *2 ; 
}

$value_1 =  1; 
echo $value_1; // 1 
conTest($value_1);
//  相当于变量的别名
echo $value_1 ; // 2 

//(3) 函数的引用

 function &conFuncTest(){ 
    static $b=0;//申明一个静态变量 
    $b=$b+1; 
    echo $b; 
    return $b; }
echo &#39;函数的引用<br>&#39; ;
$a=conFuncTest();//这条语句会输出 $b的值 为1 
$a=5; $a=conFuncTest();//这条语句会输出 $b的值 为2
$a=&conFuncTest();//这条语句会输出 $b的值 为3 

// 此时相当于$a = &$b ;  
$a=5;
// 此时 $b是$a的别名 $b = 5; 
$a=conFuncTest();
//这条语句会输出 $b的值 为6

//(4) 对象的引用,对象的引用也是为了节省空间 

class object{
	
	public  $value = &#39;Hello world&#39;; 
	
	// 如果想要clone则不让克隆 
	public  function __clone(){
		$this->value = &#39;not clone&#39;; 
	}
}


$oBject_a = new object; 
$oBject_b = $oBject_a; 
// 上面等效于 $oBject_b = &$oBject_a; 
echo $oBject_a->value;//这里输出ABC 
// 修改对象a的值,会影响b的值,因为$b 是$a的引用 
$oBject_a->value = &#39;not hello&#39;;
echo $oBject_b->value;//这里输出not hello

// 如果不想影响对象a的值,可以使用魔术方法 __clone


$obj2 = clone $oBject_a; 

echo  $obj2->value;

//(5) 变量销毁 


$a = 11 ; $b =&$a ; unset($a);  // 此时 $b = 11;  
// 相当于 


 function quoteTest(){ 
 global $var ; 
 //相当于 $var = &$GLOBALS[&#39;var&#39;]; 
 unset($var);
 //删除只是删除引用,而引用的内容还存在,同上这并不意味着变量内容被销毁了
 }
 $var=1;
 quoteTest();
 echo $var; //  
 
 
 
 
 
 
echo $wlt = 111; 
print_r($GLOBALS); 

unset($wlt) ; 

print_r($GLOBALS); 


Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/962646.htmlTechArticleApplication referenced in php;$a=conFuncTest();//This statement will output the value of $b is 1 $a=5; $a=conFuncTest();//This statement will output that the value of $b is 2$a=conFuncTest();//This statement will output...
Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template