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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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. |
|
|||
|
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/ |
|
|||
|
"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 |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|