PHP Notice: Undefined index/variable/constant isset

Posted under » PHP on 8 July 2015

You see this in your error log often. It can be irritating when in PHP4 but in PHP5 or 7, it is important to take note because this may be the reason why your code is not working.

For "Undefined index:", the cure is to use isset like so

$file_ext = isset($path_parts['extension']) ? $path_parts['extension'] : null;

What it means is that $file_ext = $path_parts['extension']. But if $path_parts['extension'] is null, then $file_ext = null.

For "Undefined variable:" or "Undefined constant", the cure is to declare it. It could also because of superglobals so you have to

$search = $_POST["search"];

Take note that brackets are important. $_POST["search"] or $_POST['search'] is not the same as $_POST[search]. In PHP7 or 8 and it will be an 'undefined constant' error.

So you can use isset in this case too.

if (isset($search)) { do this } else { do that }

if (isset($student)) {$student = "XXX"; } else {$student = $_GET["stud"]; }

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data