How to reference global variables within functions in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:48:55
Original
808 people have browsed it

First look at the following code:

Copy the code The code is as follows:

$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 '$var2'
";
global_references(true);
echo "var2 is set to '$var2'
";
?>
The output result is as follows:
var2 is set to '&&&&&'
var2 is set to '# ####'

is visible, in the above code:
$var2 =&$var1; //1
is only visible inside the function.
And
$GLOBALS["var2"] =&$var1; //2
is visible in the global scope.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319646.htmlTechArticleLook at the following code first: Copy the code as follows: ?php $var1 = "#####"; $var2 = " function global_references($use_globals) { global $var1, $var2; if (!$use_globals) { $var2 = //1...
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!