For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > August 2006 > How to get rid of warnings?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to get rid of warnings?
Lenard Redwood

2006-08-24, 9:57 pm

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.

Jerry Stuckle

2006-08-24, 9:57 pm

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
==================
Gucci

2006-08-25, 3:57 am

try
<?php
foreach ($lunch as $choice) {
print "You want a $choice bun. <br/>";
}
?>

Juliette

2006-08-25, 3:57 am

Gucci wrote:
> try
> <?php
> foreach ($lunch as $choice) {
> print "You want a $choice bun. <br/>";
> }
> ?>
>


RED ALERT ! never work with register_globals on
Manish

2006-08-25, 3:57 am

<?php

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

?>

Lenard Redwood

2006-08-25, 7:57 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

Jerry Stuckle

2006-08-25, 7:57 am

Lenard Redwood wrote:
> Manish wrote:
>
>
>
> 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
==================
Charles

2006-08-25, 7:57 am


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 :)

Chuck Anderson

2006-08-25, 6:57 pm

Jerry Stuckle wrote:
> Lenard Redwood 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.
>
>

"&& 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
*****************************
william.clarke

2006-08-27, 6:57 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

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com