Suppress notices and warning in code

This is a discussion on Suppress notices and warning in code within the PHP Language forums, part of the PHP Programming Forums category; I have a form with check boxes. When accessing a check box element that is not checked, I get a ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-25-2004
Krishna Srinivasan
 
Posts: n/a
Default Suppress notices and warning in code

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.
Reply With Quote
  #2 (permalink)  
Old 06-25-2004
Marian Heddesheimer
 
Posts: n/a
Default Re: Suppress notices and warning in code

On 24 Jun 2004 23:40:31 -0700, krishna@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/
Reply With Quote
  #3 (permalink)  
Old 06-25-2004
Markus Ernst
 
Posts: n/a
Default Re: Suppress notices and warning in code

"Krishna Srinivasan" <krishna@multimediastudio.com> schrieb im Newsbeitrag
news:361f42ca.0406242240.4172502d@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


Reply With Quote
  #4 (permalink)  
Old 06-25-2004
Krishna Srinivasan
 
Posts: n/a
Default Re: Suppress notices and warning in code

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.
Reply With Quote
  #5 (permalink)  
Old 06-25-2004
Derek Battams
 
Posts: n/a
Default Re: Suppress notices and warning in code

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
Reply With Quote
  #6 (permalink)  
Old 06-25-2004
FLEB
 
Posts: n/a
Default Re: Suppress notices and warning in code

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
Reply With Quote
  #7 (permalink)  
Old 06-27-2004
Mudge
 
Posts: n/a
Default Re: Suppress notices and warning in code

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.


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 08:44 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0