简体   繁体   English

Foreach给出未定义的变量错误

[英]Foreach giving undefined variable error

Bare in mind that it's late where I am so this may just be a very simple solution that I am overlooking. 请记住,我来晚了,所以这可能只是我所忽略的非常简单的解决方案。

I have this foreach statement: 我有这个foreach声明:

  <? foreach ($entrees as $entree) { ?>
       <li>
           <span class="food-name">
               <?=$entree['name']?>
           </span>
       </li>
  <?}?>

That gives an Undefined variable error for $entree. 这给$ entree带来了一个不确定的变量错误。

 A PHP Error was encountered

 Severity: Notice

 Message: Undefined variable: entree

 Filename: views/welcome_message.php

 Line Number: 9

I tried print_r($entrees); 我尝试了print_r($entrees); and the result was 结果是

 Array ( [0] => Array ( [name] => Lamb [description] => A small leg of lamb ) [1] => Array ( [name] => Ham [description] => Large thing of ham ) ) 1

Again, this may be something stupid simple but I've been trying to 30 min and can't get it to work for the life of me. 同样,这可能是愚蠢的简单操作,但我一直在尝试30分钟,无法让它在我的生命中发挥作用。 Thanks in advance for the help. 先谢谢您的帮助。

You need to check to see if the entry name is set in your multidimensional array. 您需要检查是否在多维数组中设置了条目名称。 Here is the long version because I was unaware of the short_open_tags option in PHP. 这是长版本,因为我不知道PHP中的short_open_tags选项。

Code

<?
$entrees = array();
$entrees[0]['name']="Lamb";
$entrees[0]['description'] ="A small leg of lamb";
$entrees[1]['name']="Ham";
$entrees[1]['description'] ="Large thing of ham";

echo "<ul>\n";
foreach ($entrees as $entree) { 
 if(!empty($entree['name'])){
   echo "<li>";
   echo "<span class=\"food-name\">";
   echo $entree['name'];
   echo "</span>";
   echo "</li>\n";
 }
}
echo "</ul>";

?>

Output 产量

<ul>
<li><span class="food-name">Lamb</span></li>
<li><span class="food-name">Ham</span></li>
</ul>

Update Learned something new thanks to @Arjan - the original code you had with the [short_open_tags][1] should also work with the if statement provided they are enabled in your build. 更新感谢@Arjan,它学到了一些新知识- if您在构建中启用了[short_open_tags][1]则原始代码也应与if语句一起使用。 In my testing they threw an error as my test machine uses an older build of PHP. 在我的测试中,由于我的测试机使用的是旧版本的PHP,因此引发了错误。 After rebuilding they do work by default. 重建后,它们会默认工作。

<?=$entree['name']?>

According to the manual in this usage the = sign means echo() and is enabled by default in PHP 5.4.1. 根据这种用法的手册=符号表示echo() ,默认情况下在PHP 5.4.1中启用。 Upon searching through Google because their searches use the operators differently I was unable to find a link since I did not know the name for the usage. 在搜索Google时,因为他们的搜索使用的运算符不同,所以我找不到链接,因为我不知道用法的名称。 If you're programming with a team of developers make sure to use the same agreed upon tag styles as the rest of the team. 如果您与一组开发人员一起编程,请确保使用与其他团队相同的商定标签样式。

You need to set short_open_tag to On in php.ini. 您需要在php.ini中将short_open_tag设置为On That fixed it for me with no additional code. 这样就为我解决了此问题,无需任何其他代码。

Use like .. 使用像..

   <?   
     if(isset($entrees)) {
     foreach ($entrees as $entree) { ?>
   <li>
       <span class="food-name">
           <?=$entree['name']?>
       </span>
   </li>
      <?} } 
       else { echo 'Any thing You want';
      }
     ?>

try this 尝试这个

<?php 
   if(isset($entrees) && $entrees != NULL){
      foreach($entrees as $entree => $arrData){?>
        <li>
          <span class="food-name">
            <?php echo $arrData['name'] ?>
          </span>
        </li>         
     <?}
   }?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM