请记住,我现在已经很晚了,所以这可能只是我忽略的一个非常简单的解决方案。
我有这个 foreach 语句:
<? foreach ($entrees as $entree) { ?>
<li>
<span class="food-name">
<?=$entree['name']?>
</span>
</li>
<?}?>
这给出了 $entree 的 undefined variable 错误。
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: entree
Filename: views/welcome_message.php
Line Number: 9
我尝试了 print_r($entrees);
结果是
Array ( [0] => Array ( [name] => Lamb [description] => A small leg of lamb ) [1] => Array ( [name] => Ham [description] => Large thing of ham ) ) 1
同样,这可能是一件愚蠢简单的事情,但我一直在尝试 30 分钟,但无法让它在我的生活中发挥作用。在此先感谢您的帮助。
最佳答案
您需要在 php.ini 中将 short_open_tag
设置为 On
。这为我修复了它,没有额外的代码。
关于php - Foreach 给出 undefined variable 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16411726/