f



How to get rid of warnings?

I have the maximum number of warnings during PHP development. How can I
get rid of these warnings without turning them off in PHP?

Here's the code I have:

<form method="POST" action="eat.php">
<select name="lunch[  ]" multiple>
<option value="pork">BBQ Pork Bun</option>
<option value="chicken">Chicken Bun</option>
<option value="lotus">Lotus Seed Bun</option>
<option value="bean">Bean Paste Bun</option>
<option value="nest">Bird-Nest Bun</option>
</select>
<input type="submit" name="submit">
</form>

Selected buns: <br/>
<?php
foreach ($_POST['lunch'] as $choice) {
    print "You want a $choice bun. <br/>";
}
?>


And I get: Warning: Invalid argument supplied for foreach() in
C:\lighttpd\htdocs\index.php on line 17

Is there a way to give the 'lunch' variable a default value if
undefined?
Thank you.

0
8/25/2006 2:46:07 AM
comp.lang.php 32646 articles. 0 followers. Post Follow

9 Replies
885 Views

Similar Articles

[PageSpeed] 35

Lenard Redwood wrote:
> I have the maximum number of warnings during PHP development. How can I
> get rid of these warnings without turning them off in PHP?
> 
> Here's the code I have:
> 
> <form method="POST" action="eat.php">
> <select name="lunch[  ]" multiple>
> <option value="pork">BBQ Pork Bun</option>
> <option value="chicken">Chicken Bun</option>
> <option value="lotus">Lotus Seed Bun</option>
> <option value="bean">Bean Paste Bun</option>
> <option value="nest">Bird-Nest Bun</option>
> </select>
> <input type="submit" name="submit">
> </form>
> 
> Selected buns: <br/>
> <?php
> foreach ($_POST['lunch'] as $choice) {
>     print "You want a $choice bun. <br/>";
> }
> ?>
> 
> 
> And I get: Warning: Invalid argument supplied for foreach() in
> C:\lighttpd\htdocs\index.php on line 17
> 
> Is there a way to give the 'lunch' variable a default value if
> undefined?
> Thank you.
> 

Make sure $_POST['lunch'] is set and is an array.  Check out isset() and 
is_array().

And ALWAYS validate information coming from the user.  NEVER assume 
you're getting something.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
0
jstucklex (14659)
8/25/2006 3:18:37 AM
try
 <?php
foreach ($lunch as $choice) {
    print "You want a $choice bun. <br/>";
}
?>

0
jueljust (9)
8/25/2006 4:06:03 AM
Gucci wrote:
> try
>  <?php
> foreach ($lunch as $choice) {
>     print "You want a $choice bun. <br/>";
> }
> ?>
> 

RED ALERT ! never work with register_globals on
0
8/25/2006 5:51:05 AM
<?php

if(is_array($_POST['lunch']) && count($_POST['lunch'])) {
    foreach ($_POST['lunch'] as $choice) {
        print "You want a $choice bun. <br/>";
    }
}

?>

0
8/25/2006 6:59:33 AM
Manish wrote:
> <?php
>
> if(is_array($_POST['lunch']) && count($_POST['lunch'])) {
>     foreach ($_POST['lunch'] as $choice) {
>         print "You want a $choice bun. <br/>";
>     }
> }
>
> ?>

Thank you. Now I get this error:

Notice: Undefined index: lunch in C:\lighttpd\htdocs\index.php on line
17

0
8/25/2006 12:43:08 PM
Lenard Redwood wrote:
> Manish wrote:
> 
>><?php
>>
>>if(is_array($_POST['lunch']) && count($_POST['lunch'])) {
>>    foreach ($_POST['lunch'] as $choice) {
>>        print "You want a $choice bun. <br/>";
>>    }
>>}
>>
>>?>
> 
> 
> Thank you. Now I get this error:
> 
> Notice: Undefined index: lunch in C:\lighttpd\htdocs\index.php on line
> 17
> 

See my earlier suggestion.  Before checking if it's an array, you should 
see if it's even set:

if(isset($_POST['lunch']) && is_array($_POST['lunch']) && 
count($_POST['lunch'])) {
     foreach ($_POST['lunch'] as $choice) {
         print "You want a $choice bun. <br>";
     }
}

BTW - <br/> is valid for xml but not html.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
0
jstucklex (14659)
8/25/2006 12:57:26 PM
Jerry Stuckle wrote:
> See my earlier suggestion.  Before checking if it's an array, you should
> see if it's even set:
>
> if(isset($_POST['lunch']) && is_array($_POST['lunch']) &&
> count($_POST['lunch'])) {
>      foreach ($_POST['lunch'] as $choice) {
>          print "You want a $choice bun. <br>";
>      }
> }


Yes! Thank you Jerry! Working great now :)

0
landemaine (50)
8/25/2006 1:12:39 PM
Jerry Stuckle wrote:
> Lenard Redwood wrote:
>   
>> Manish wrote:
>>
>>     
>>> <?php
>>>
>>> if(is_array($_POST['lunch']) && count($_POST['lunch'])) {
>>>    foreach ($_POST['lunch'] as $choice) {
>>>        print "You want a $choice bun. <br/>";
>>>    }
>>> }
>>>
>>> ?>
>>>       
>> Thank you. Now I get this error:
>>
>> Notice: Undefined index: lunch in C:\lighttpd\htdocs\index.php on line
>> 17
>>
>>     
>
> See my earlier suggestion.  Before checking if it's an array, you should 
> see if it's even set:
>
> if(isset($_POST['lunch']) && is_array($_POST['lunch']) && 
> count($_POST['lunch'])) {
>      foreach ($_POST['lunch'] as $choice) {
>          print "You want a $choice bun. <br>";
>      }
> }
>
> BTW - <br/> is valid for xml but not html.
>
>   
"&& count($_POST['lunch'])" seems superfluous here. If it's an array 
(empty or not), then foreach will not issue a warning.

-- 
*****************************
 Chuck Anderson � Boulder, CO
 http://www.CycleTourist.com
*****************************
0
8/25/2006 2:18:07 PM
Jerry Stuckle wrote:
> See my earlier suggestion.  Before checking if it's an array, you should
> see if it's even set:
>
> if(isset($_POST['lunch']) && is_array($_POST['lunch']) &&
> count($_POST['lunch'])) {
>      foreach ($_POST['lunch'] as $choice) {
>          print "You want a $choice bun. <br>";
>      }
> }
>
> BTW - <br/> is valid for xml but not html.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstucklex@attglobal.net
> ==================

<br /> for XHTML
<br> for HTML

0
8/27/2006 10:42:41 PM
Reply: