从另一个片段调用函数时出现未定义的变量错误 [英] Undefined variable error when calling a function from another snippet
本文介绍了从另一个片段调用函数时出现未定义的变量错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下 PHP 页面
<?php
$a = "World";
function say() {
echo $a;
}
?>
Hello, <?php say(); ?>
失败:
Undefined variable: a in test.php on line 5
有人能解释一下原因吗?解决这个问题的最佳方法是什么?
Could someone explain me why, and what is the best way to fix this?
推荐答案
你必须在函数内部将变量定义为全局变量
you have to define variable as global inside the function
<?php
$a = "World";
function say() {
global $a;
echo $a;
}
say();
?>
这篇关于从另一个片段调用函数时出现未定义的变量错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文