...

View Full Version : Get rid of notices



guvenck
11-25-2006, 08:15 PM
Hi,

I guess my php.ini has error reporting to all. Therefore I get many notices such as Notice: undefined variable or Notice: undefined index etc. I know I could use the ini_set command and not get these errors or I could add



if(isset($variable)) {
if($variable != "") { echo "$variable"; } ?>
}


to each $variable related line but I don't know if this is necessary. My question: Why do I get such notices? Second, if I use ini_set, are the notices generated anyway but not printed on screen? Does that bring performance loss? Should I stop notices in my code?

chump2877
11-25-2006, 08:36 PM
Using error reporting (http://us2.php.net/error_reporting) or ini_set at the top of your script file will nix the notices.

Do you have access to the php.ini....I would try to change the setting in there, so you don;t have to do the above for all of your files (http://us2.php.net/manual/en/ref.errorfunc.php#ini.error-reporting)...

you can also do a php_info() and see your server configuration....you can see what error_reporting is set to...

If you're on a shared server, ask your hosting provider...

guvenck
11-25-2006, 09:23 PM
Thanks for the answer. My error reporting is set to 2039 (says phpinfo()). I know I can easily turn off error reporting whether with PHP or with .htaccess (php_flag display_errors off). I wonder if there is a way to stop notices IN the code. For example:

$my_id = $_SESSION['id'];

generates a:

Notice: Undefined index id

notice. I can't understand that. It is obvious that it is undefined. How to define id?

CFMaBiSmAd
11-25-2006, 10:21 PM
Even if error reporting is set so that the notices and warnings are not output to the browser, they are still logged. This will cause a minor speed penalty when the page is parsed and severed.

Proper programming and testing will eliminate the notices and warnings. For your example -

session_start();
$my_id = $_SESSION['id'];
// if the session variable does not exist, $my_id will be null at this point
// and any following code that uses it will fail, such as a mysql query...If this session variable does not exist (but your site does make use of this elsewhere) that would indicate that someone reached the page that has this code without properly visiting the page that creates this session variable. Doing something like the following would be appropriate -

session_start();
if(!isset($_SESSION['id'])) {
// note the ! (not) in the above line
die('You are not permitted to access this page directly');
}
// normal page code here...
$my_id = $_SESSION['id'];
// there will be a value in the variable at this point and code that depends
// on it being set from a previous page will not fail due to it being a null value

If you have one page that both sets a variable if it does not exist and then tests it, the same concept will work -

session_start();
if(!isset($_SESSION['id'])) {
// note the ! (not) in the above line
$_SESSION['id'] = "your id value";
}
// normal page code here...
$my_id = $_SESSION['id'];
// there will be a value in the variable at this point and code that depends
// on it being set will not fail due to it being a null value



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum