How To Remove Undefined Index Error in PHP Variables

$ _POST, $ _GET or $_REQUEST are the PHP functions that are used to get variables from a user-filled form or user requested pages. While using these functions, a user may encounter undefined index error.

How to fix this error?

Undefined index is a minor error and this error can be avoided with the help of PHP isset ().
You can hide this notice with the help of the error_reporting function. Simply make error_reoprting(0) to hide all notifications. Add the following code to the first line of your php code.

<?php
error_reoprting(0); 
?>

Fix Undefined Index Error

<?php
$name = $_POST['name']; /* This may show Undefined index error. */
?>

Change this code to

<?php
if (isset($_POST['name'])) {
    $name = $_POST['name'];
}
?>

Same for $_GET['value'] and $_REQUEST['value']
<?php
if (isset($_GET['value'])) {
    $var = $_GET['value'];
}
//or
if (isset($_REQUEST['value'])) {
    $var = $_REQUEST['value'];
}

?>

Other Related Posts

MySQL deprecated error
PHP Error Handling
All deprecated functions in php 5.3 onwards



Top