Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ($entrees as $entree) { ?>
       <li>
           <span class="food-name">
               <?=$entree['name']?>
           </span>
       </li>
  <?}?>

That gives an Undefined variable error for $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); 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. Thanks in advance for the help.

like image 769
Dylan Buth Avatar asked Oct 20 '25 01:10

Dylan Buth


1 Answers

You need to set short_open_tag to On in php.ini. That fixed it for me with no additional code.

like image 100
Salaryman Avatar answered Oct 21 '25 15:10

Salaryman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!