ShoeLace1291
ShoeLace1291

Reputation: 4698

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.

Upvotes: 4

Views: 12771

Answers (5)

Paul V
Paul V

Reputation: 359

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.

Upvotes: 0

Tommy
Tommy

Reputation: 1

Try this:

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

Upvotes: 0

RiaD
RiaD

Reputation: 47619

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');

Upvotes: 3

swordfish
swordfish

Reputation: 4995

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

Upvotes: 2

Ravi
Ravi

Reputation: 737

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.

Upvotes: 0

Related Questions