PHP Notice: Undefined index/variable 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:", the cure is to declare it. It could also because of superglobals so you have to

$search = $_POST["search"];

Take note that $_POST["search"] or $_POST['search'] is not the same as $_POST[search]. In PHP8? it will be an error.

So you can use isset in this case too.

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

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