PHP中函数内引用全局变量的方法_php技巧

PHP中文网
Release: 2016-05-17 09:34:36
Original
1525 people have browsed it

PHP中函数内引用全局变量的方法_php技巧

先看下面的代码:

 代码如下:

<?php 
$var1 = "#####"; 
$var2 = "&&&&&"; 
function global_references($use_globals) 
{ 
global $var1, $var2; 
if (!$use_globals) { 
$var2 =&$var1; //1 
} else { 
$GLOBALS["var2"] =&$var1; //2 
} 
} 
global_references(false); 
echo "var2 is set to &#39;$var2&#39;
"; 
global_references(true); 
echo "var2 is set to &#39;$var2&#39;
"; 
?>
Copy after login

输出的结果如下:

var2 is set to &#39;&&&&&&#39; 
var2 is set to &#39;#####&#39;
Copy after login

可见,上面的代码中:

$var2 =&$var1; //1
Copy after login


只对函数内部可见。

$GLOBALS["var2"] =&$var1; //2
Copy after login


在全局范围内可见。

以上就是PHP中函数内引用全局变量的方法_php技巧的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!


Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!