[PHP] proper method to do the following...
I've been working with error_reporting(E_ALL) set lately trying to write code that does not give notices or errors. I have not been able to solve the following: given a form with a checkbox that may or may not be set, we'll call it exact as in input type = checkbox name = exact the following code outputs : Notice: Undefined index: exact in search.php on line 10 code : if (!isset($_POST['exact'])) { - line 10 $_POST['exact'] == false; } if ($_POST['exact'] == checked) -- also here { $exact_match_only = true; } else { $exact_match_only = false; } I understand why it is giving the notice but I'm not sure how to make it stop. What is the proper way to set a variable so that it's non-existance will not generate a notice? Any ideas? Let me know if the above is not clear. Thanks. ___ Do you Yahoo!? Declare Yourself - Register online to vote today! http://vote.yahoo.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] proper method to do the following...
Hugh Beaumont wrote: I've been working with error_reporting(E_ALL) set lately trying to write code that does not give notices or errors. I have not been able to solve the following: given a form with a checkbox that may or may not be set, we'll call it exact as in input type = checkbox name = exact the following code outputs : Notice: Undefined index: exact in search.php on line 10 code : if (!isset($_POST['exact'])) { - line 10 $_POST['exact'] == false; } if ($_POST['exact'] == checked) -- also here { $exact_match_only = true; } else { $exact_match_only = false; } I understand why it is giving the notice but I'm not sure how to make it stop. What is the proper way to set a variable so that it's non-existance will not generate a notice? Any ideas? Let me know if the above is not clear. Thanks. Try it like this... if ( isset ( $_POST['exact'] ) ) { // do what you need to do if it's checked } else { // do what you need to do if it's not checked } -- John C. Nichel ÜberGeek KegWorks.com 716.856.9675 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] proper method to do the following...
On Wed, 6 Oct 2004 06:53:34 -0700 (PDT), Hugh Beaumont [EMAIL PROTECTED] wrote: Notice: Undefined index: exact in search.php on line 10 code : if (!isset($_POST['exact'])) { - line 10 $_POST['exact'] == false; } Reverse the logic of isset(). if (isset($_POST['exact'])) { $_POST['exact'] == TRUE; } If a variable is genuinely not set, it will produce the warning or notice you are seeing. There are a few ways to circumvent that: lower your error reporting level, or quiet the error with @, or reverse the logic so the error doesn't exist. I prefer the last method as I always use full error reporting during development. -- Greg Donald Zend Certified Engineer http://gdconsultants.com/ http://destiney.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] proper method to do the following...
I've been working with error_reporting(E_ALL) set lately trying to write code that does not give notices or errors. What is the proper way to set a variable so that it's non-existance will not generate a notice? Thanks all for the replies. I also noticed a small typo in the code I posted which made it even worse (used == instead of = when doing the assignment). Thanks again! __ Do you Yahoo!? Yahoo! Mail Address AutoComplete - You start. We finish. http://promotions.yahoo.com/new_mail -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] proper method to do the following...
Hugh Beaumont wrote: the following code outputs : Notice: Undefined index: exact in search.php on line 10 code : if (!isset($_POST['exact'])) { - line 10 $_POST['exact'] == false; } You have a typo in line 11. I'm assuming you want to use the assignment operator = instead of the equality operator ==. The PHP parser seems to be incorrectly attributing the error to line 10 when it should be on line 11. If you change == to = your error will go away. It took me a moment to catch it because of the misleading error message. HTH -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] proper method to do the following...
Hugh Beaumont [EMAIL PROTECTED] wrote: quote I've been working with error_reporting(E_ALL) set lately trying to write code that does not give notices or errors. What is the proper way to set a variable so that it's non-existance will not generate a notice? Thanks all for the replies. I also noticed a small typo in the code I posted which made it even worse (used == instead of = when doing the assignment). quote There's a book called XX number of ways to improve your C++ code. Well, it works on lots of languages. About equality statements? Put the constant on the left, then it's not accidentally an assignement. if( 15==$dudes_age ){ $action=Slap him if he looks at your daughter; } vs if( $daughters_age==(18*365 + 4 + 1) ){ $action=Give her a box of con***s and kick her out; } //18 years, plus 4 leap days plus one day. If you accidentally mamke the '==' a '=' in the second statement, it will always be true, and you will get arrested for kicking your 8 year old out into the street. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php