473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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.

Aug 25 '06 #1
9 2187
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.
js*******@attglobal.net
==================
Aug 25 '06 #2
try
<?php
foreach ($lunch as $choice) {
print "You want a $choice bun. <br/>";
}
?>

Aug 25 '06 #3
Gucci wrote:
try
<?php
foreach ($lunch as $choice) {
print "You want a $choice bun. <br/>";
}
?>
RED ALERT ! never work with register_globals on
Aug 25 '06 #4
<?php

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

?>

Aug 25 '06 #5

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

Aug 25 '06 #6
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.
js*******@attglobal.net
==================
Aug 25 '06 #7

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

Aug 25 '06 #8
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
*****************************
Aug 25 '06 #9

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.
js*******@attglobal.net
==================
<br /for XHTML
<brfor HTML

Aug 27 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Kylotan | last post by:
I have the following code: def IntToRandFloat(x): """Given a 32-bit integer, return a float in """ x = int(x) x = int(x << 13) ^ x return...
12
by: Gary | last post by:
Hi! guys, I have a SQL agent job fails because it gets 10 warnings when it runs a stored procedure. These warnings are trivial and can be ignored. Can I make it ignore these warnings and...
3
by: Terry Richards | last post by:
mysql 4.1.9-standard how do i find exactly what the warnings are when i only get Query OK, 1 row affected, 1 warning (0.00 sec) :-)^2
30
by: prasanna | last post by:
i will be very thankful if you sent all the errors and warnings regarding to the language C thank you
22
by: John Fisher | last post by:
void f(int p) { } Many (most?) compilers will report that p is unreferenced here. This may not be a problem as f may have to match some common prototype. Typically pointers to functions are...
2
by: funkyj | last post by:
I've been googling around trying to find the answer to this question but all I've managed to turn up is a 2 year old post of someone else asking the same question (no answer though). ...
6
by: pete142 | last post by:
When I compile this code: typedef unsigned char BYTE; BYTE * IpString(unsigned int ip) { static BYTE ipString; ipString = (BYTE) 0xff & (ip >24); ipString = (BYTE) 0xff & (ip >16);
3
by: gil | last post by:
Hi, I'm trying to find the best way to work with compiler warnings. I'd like to remove *all* warnings from the code, and playing around with the warning level, I've noticed that compiling with...
1
by: billiejoex | last post by:
Hi there, into a module of mine I 'warn' a message if a certain situation occurs: def add_anonymous_user(permissions=('r'): if 'w' in p: import warnings warnings.warn("it's not rencommended...
1
by: Robert Singer | last post by:
Platform: winXP, excel 2003 Python 2.5.2 XLWriter 0.4a3 (http://sourceforge.net/projects/pyxlwriter/) Is anyone here using this very nice package, for writing excel files? I'm using it on...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.