简体   繁体   中英

Undefined Index Error Reporting in WAMP and PHP

I'm using wamp to develop a php application. My problem is that everytime I call a variable that sometimes happens to not have a value, I get an error that says it's an undefined index. Is there a way to change the error reporting to not display this error? I have to use isset to determine if it's set or not before I output the variable, but I don't want to have to do this. There are areas of my application that make this method inefficient.

If you don't want to change error_reporting level you should check, is variable exists, before using it. You may use

 if(isset($var)) 

for it. You may add some function, to not write it always. Example:

 function getPost($name,$default=null){
     return isset($_POST[$name])?$_POST[$name]:$default;
 }

Usage:

getPost('id');
getPost('name','Not Logged In');

You can just turn off the mechanism in php.ini.

This thread would help you.

http://www.wampserver.com/phorum/read.php?2,70609,70700

But it generally its better to take care of undefined variables as they might save you some run time trouble.

Update:

In php.ini change

error_reporting = E_ALL to error_reporting = E_ALL & ~E_NOTICE

Try this:

if(!isset($var)) $var="";

PHP.ini files reside in both :

bin\\php\\php5.x

and

bin\\apache\\apache2.x\\bin

be sure to make the changes in the apache folder version .


Also setting :

display_errors = Off

display_startup_errors = Off

error_reporting = E_ALL

log_errors = On

leaves errors from being displayed on the client, but still allows them to be logged in the error log.

There are multiple ways to get around this:
error_reporting(0) Use this at the top of your script
set display_errors = Off in php.ini
Use '@' before the statement that generates an error

But unless you are writing something trivial you absolutely must use array_key_exists or if(!empty($arrayName['key'])) for everything sent by the user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM