开发者

Undefined Index Error Reporting in WAMP and PHP

开发者 https://www.devze.com 2023-03-31 15:54 出处:网络
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开发者_StackOverflow社区 an undefi

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开发者_StackOverflow社区 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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号