php 函数内引用全局变量
<?php$var1="Examplevariable";$var2="";functionglobal_references($use_globals){global$...
<?php
$var1 = "Example variable";
$var2 = " ";
function global_references($use_globals)
{
global $var1, $var2;
if (!$use_globals) {
$var2 =& $var1; // visible only inside the function///???
} else {
$GLOBALS["var2"] =& $var1; // visible also in global context
}
}
global_references(false);
echo "var2 is set to '$var2'\n"; // var2 is set to ''
global_references(true);
echo "var2 is set to '$var2'\n"; // var2 is set to 'Example variable'
?>
把 global $var2; 当成是 $var2 =& $GLOBALS['var2']; 的简写。从而将其它引用赋给 $var2 只改变了本地变量的引用。
把 global $var2; 当成是 $var1 =& $GLOBALS['var1']; 的简写。从而将其它引用赋给 $var2 只改变了本地变量的引用。
问:$var2 =& $var1; // visible only inside the function///???这句虽然改变了var2的引用,可这个引用是va1啊,va1不是引用外部变量吗,所以va2是也应是引用外部变量到了外部应仍然有用,怎么只能在内部用。 展开
$var1 = "Example variable";
$var2 = " ";
function global_references($use_globals)
{
global $var1, $var2;
if (!$use_globals) {
$var2 =& $var1; // visible only inside the function///???
} else {
$GLOBALS["var2"] =& $var1; // visible also in global context
}
}
global_references(false);
echo "var2 is set to '$var2'\n"; // var2 is set to ''
global_references(true);
echo "var2 is set to '$var2'\n"; // var2 is set to 'Example variable'
?>
把 global $var2; 当成是 $var2 =& $GLOBALS['var2']; 的简写。从而将其它引用赋给 $var2 只改变了本地变量的引用。
把 global $var2; 当成是 $var1 =& $GLOBALS['var1']; 的简写。从而将其它引用赋给 $var2 只改变了本地变量的引用。
问:$var2 =& $var1; // visible only inside the function///???这句虽然改变了var2的引用,可这个引用是va1啊,va1不是引用外部变量吗,所以va2是也应是引用外部变量到了外部应仍然有用,怎么只能在内部用。 展开
2个回答
展开全部
<?php
$var1 = "Example variable";
$var2 = " ";
function global_references($use_globals)
{
global $var1, $var2;
if (!$use_globals) {
$var2 =& $var1; // visible only inside the function///???
} else {
$GLOBALS["var2"] =& $var1; // visible also in global context
}
}
global_references(false);//变量$var2赋值为对$var1的引用,但是它的值在函数外不可见,他是一个局部变量
echo "var2 is set to '$var2'\n<br>";
global_references(true);//将$var2注册到全局变量数组中赋值为对变量$var1的引用,他是全局可见的
echo "var2 is set to '$var2'\n";
?>
$var1 = "Example variable";
$var2 = " ";
function global_references($use_globals)
{
global $var1, $var2;
if (!$use_globals) {
$var2 =& $var1; // visible only inside the function///???
} else {
$GLOBALS["var2"] =& $var1; // visible also in global context
}
}
global_references(false);//变量$var2赋值为对$var1的引用,但是它的值在函数外不可见,他是一个局部变量
echo "var2 is set to '$var2'\n<br>";
global_references(true);//将$var2注册到全局变量数组中赋值为对变量$var1的引用,他是全局可见的
echo "var2 is set to '$var2'\n";
?>
参考资料: phptogether.com
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询