如何解决为什么使用返回函数时全局变量显示未定义?
各位,
我正在学习全局变量和超级全局变量。一直在实验。 以下作品。我能够'返回 $GLOBALS['z4']'。
示例 1
<?php
//Php holds all 'Global Variables' in an array. Format: $GLOBALS[index]; The 'index' holds the name of the variable.
$x4 = 4; //'Global Variable' as outside function. It now exists in "Super Global" Variable array as: $GLOBALS['x4'].
$y4 = 4; //'Global Variable' as outside function. It now exists in "Super Global" Variable array as: $GLOBALS['y4'].
function addition_4()
{
$GLOBALS['z4'] = $GLOBALS['x4'] + $GLOBALS['y4']; //A new "Global Variable" '$z4' is created inside the function.
return $GLOBALS['z4'];
}
echo addition_4();
?>
但是在下面的这个中,为什么我无法'return $z4'?
示例 2
<?php
//Php holds all 'Global Variables' in an array. Format: $GLOBALS[index]; The 'index' holds the name of the variable.
$x4 = 4; //'Global Variable' as outside function. It now exists in "Super Global" Variable array as: $GLOBALS['x4'].
$y4 = 4; //'Global Variable' as outside function. It now exists in "Super Global" Variable array as: $GLOBALS['y4'].
function addition_4()
{
$GLOBALS['z4'] = $GLOBALS['x4'] + $GLOBALS['y4']; //A new "Global Variable" '$z4' is created inside the function.
return $z4; //WHY I GET ERROR: "Notice: Undefined variable: z4 in ..." ?
}
echo addition_4();
?>
伙计们,由于在上面的两个示例中都存在以下内容“$GLOBALS['z4']”,那么我能够在第一个示例中“返回 $GLOBALS['z4']。
现在,既然 "$GLOBALS['z4']" 存在于两个例子中,那么这也意味着 "$z4" 也存在于两个例子中。正确的 ? 那么,为什么我无法在第二个示例中“返回 $z4”?为什么我收到错误,$z4 未定义?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。