Undefined Index Error Reporting in WAMP and PHP

ShoeLace1291 picture ShoeLace1291 · Aug 29, 2011 · Viewed 11.4k times · Source

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.

Answer

RiaD picture RiaD · Aug 29, 2011

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