Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error Reporting - can't turn Notices off!

Status
Not open for further replies.

groovygarden

Programmer
Aug 23, 2001
63
Hi, I hope someone can tell me what (hopefully really obvious thing) I'm doing wrong. I have a page results.php which processes results from a form. This is a bit of the code:
Code:
  if (!$searchtype || !$searchterm)
  {
     echo "You have not entered search details.  Please go back and try again.";
     exit;
  }
Yesterday I was getting the following notice:
Code:
Notice: Undefined variable: searchterm
Notice: Undefined variable: searchtype
which was fair enough because I had error reporting set to E_ALL. But I've changed error reporting to
Code:
error_reporting  = E_ALL & ~E_NOTICE
in php.ini and I'm still getting the notice.... Is there something else I need to do?
 
Two approaches, depending on exactly what you're accomplishing here... do searchtype and searchterm equal true and false or do you just want to know if they're set?...

if (!@$searchtype || !@$searchterm)
{
blah blah
}

or

if (!isset($searchtype)) {
$searchtype = false;
}
if (!isset($searchterm)) {
$searchterm = false;
}
if (!$searchtype || !$searchterm)
{
blah blah
}

-Rob
 
groovygarden:

It is extraordinarily bad programming practice to just turn off offending script errors rather than programming your way around them. It's the ethical equivalent of slapping a coat of paint on a piece of rotten wood.

skiflyer has hit the nail on the head in his second piece of advice. The first is just a way of doing locally what you have done globally. I recommend the use of the "@" error supression mechanism only in situations where you are trapping and handling reporting of all errors yourself. Want the best answers? Ask the best questions: TANSTAAFL!
 
So, at the end of the day what is the industry recognised process with regard to initialising variables ? Is it :-

i) turn off E_NOTICE
ii) initialise variables ie $somevar = "";
iii) use isset() as per 'groovygarden' on Jan 30, 2003

Bearing in mind that with ii) & iii) there could be numerous variables to initialise (or are there not [wink])

Thanks and Regards,
 
Turning off E_NOTICE is the worst of all the ideas -- the easiest to implement, but the worst idea. It is an inadequate substitute for good error-handling.

As far as I know, there is no industry-recognized standard because languages differ too much in how they handle variables. For example, c doesn't care that you've initialized it, only that you have declared it. perl, unless you use the "use strict" pragma, doesn't care one way or the other.

In my code, I use a combination of methods 2 and 3. Any variable I am going to instantiate and use in my code, I initialize. Any variable that may or may not exist (like elements of $_POST or $_GET) I check with isset() or array_key_exists().

Want the best answers? Ask the best questions: TANSTAAFL!
 
Sleipnir214,

Many thanks for your advice, even if it does up the work involved [dazed]

Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top