How to Fix PHP Notice: Undefined index?

In PHP, we lot of time face the "Warning: Undefined index", when the ‘Undefined index’ notice occurs, most of the time it is a good practice to fix this than ignoring.


By doing simple thing we can resolve this issue, so lets fix it.


One simple answer – isset() !


eg:
<!--?php
$notify = "";
$uName = $_POST['username'];
$eMail = $_POST['email'];
$comment= $_POST['comment'];
if(isset($_POST['notify_box'])){ $notify = $_POST['notify_box']; }
// send the data to the database
?>


The same fix can be used for the above warning when working with $_SESSION, $_POST arrays.
But, there instances where harmless notices can be ignored.


We can fix this using the isset() function too. But on this instance, we can just ignore it by hiding the notices like this.

error_reporting(E_ALL ^ E_NOTICE);


You can also turn off error reporting in your php.ini file or .htaccess file, but it is not considered as a wise move if you are still in the testing stage.
This is another simple solution in PHP for a common complex problem. Hope it is useful.


Refer Help : siliconstation.com

Post a Comment

Post a Comment (0)

Previous Post Next Post