By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,965 Members | 1,702 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,965 IT Pros & Developers. It's quick & easy.

Suppress notices and warning in code

P: n/a
I have a form with check boxes. When accessing a check box element
that is not checked, I get a notice (Notice: Undefined variable..). Is
there a way to hide these notices and warning in PHP code without
having to modify the PHP.ini file?

Krishna Srinivasan.
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 24 Jun 2004 23:40:31 -0700, kr*****@multimediastudio.com (Krishna
Srinivasan) wrote:
I have a form with check boxes. When accessing a check box element
that is not checked, I get a notice (Notice: Undefined variable..). Is
there a way to hide these notices and warning in PHP code without
having to modify the PHP.ini file?


you can enter this line at the top of your code:

error_reporting (E_ALL ^ E_NOTICE);

Regards

Marian

--
Tipps und Tricks zu PHP, Coaching und Projektbetreuung
http://www.heddesheimer.de/coaching/
Jul 17 '05 #2

P: n/a
"Krishna Srinivasan" <kr*****@multimediastudio.com> schrieb im Newsbeitrag
news:36**************************@posting.google.c om...
I have a form with check boxes. When accessing a check box element
that is not checked, I get a notice (Notice: Undefined variable..). Is
there a way to hide these notices and warning in PHP code without
having to modify the PHP.ini file?


You can define which errors will be displayed with the function
error_reporting(). Check the manual for the details. It is a good thing to
write this function at the top of the code, if you use an include file for
db settings and stuff like that, add it there, so you can change it globally
for the whole application.

Anyway it is better to change your code the way that no errors occur:

if(isset($_POST['mycheckbox'])) {
$mycheckbox = $_POST['mycheckbox'];
}
else {
$mycheckbox = "";
}

I usually have error_reporting(E_ALL) for seeing all kinds of warnings and
notices while developing. When going online I change it to
error_reporting(0) to hide errors (that actually should not even occur now,
but you never know...).

HTH
Markus
Jul 17 '05 #3

P: n/a
It's wired how almost immediately after I posted this message I got a
sitepoint tech times newsletter which contained an article on
suppressing these messages.

Thanks for nothing.
Krishna Srinivasan.
Jul 17 '05 #4

P: n/a
Krishna Srinivasan wrote:
I have a form with check boxes. When accessing a check box element
that is not checked, I get a notice (Notice: Undefined variable..). Is
there a way to hide these notices and warning in PHP code without
having to modify the PHP.ini file?

Krishna Srinivasan.


I'd suggest not suppressing the error and rather changing your code to
account for the fact that someone may not check a given box on your
form. The undefined variable notice is much like a compiler warning in
C/C++. Unfortunately, the programmers I deal with in C/C++ ignore the
errors much like you're trying to suppress the PHP notice, which usually
ends up in some type of error further down the road.

Instead, I'd suggest checking for the value before attempting to use it:

if(isset($_POST['var_name']))//User checked the box (sub $_GET if req'd)
{
// Do whatever...
}
else // User didn't check the box
{
// Do whatever... lose the else block if you need to do nothing
}

Any PHP project I manage the first rule is, warnings/notices are errors.
I only allow the suppression of warnings when the warning is dealt
with in the code (for example, I'll usually suppress the warnings from
fopen since my code explicitly deals with failures).

HTH,

Derek
Jul 17 '05 #5

P: n/a
Regarding this well-known quote, often attributed to Krishna Srinivasan's
famous "24 Jun 2004 23:40:31 -0700" speech:
I have a form with check boxes. When accessing a check box element
that is not checked, I get a notice (Notice: Undefined variable..). Is
there a way to hide these notices and warning in PHP code without
having to modify the PHP.ini file?

Krishna Srinivasan.


You could also use
array_key_exists('checkbox_name', $_GET);
(or $_POST, whatever) to determine whether the variable exists.

--
-- Rudy Fleminger
-- sp@mmers.and.evil.ones.will.bow-down-to.us
(put "Hey!" in the Subject line for priority processing!)
-- http://www.pixelsaredead.com
Jul 17 '05 #6

P: n/a
Krishna Srinivasan wrote:
I have a form with check boxes. When accessing a check box element
that is not checked, I get a notice (Notice: Undefined variable..). Is
there a way to hide these notices and warning in PHP code without
having to modify the PHP.ini file?

Krishna Srinivasan.


Have you tried using the "@" symbol when you try to access the variable?
The @ suppresses errors if you put it in front of your code. For instance

@$cat = $_POST['cat'];

would suppress any errors if you couldn't do this.
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.