Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
以下 PHP 页面
<?php $a = "World"; function say() { echo $a; } ?> Hello, <?php say(); ?>
失败:
Undefined variable: a in test.php on line 5
有人能解释一下为什么吗,解决这个问题的最佳方法是什么?
您必须在函数内将变量定义为全局变量
<?php $a = "World"; function say() { global $a; echo $a; } say(); ?>