Undefined Index error

I am working on an LBF form that will not save on an Ubuntu box.

Ubuntu 16.04.5 LTS

MySQL 5.7.24

PHP 7.02

The form was originally built on a Windows server then database was transferred to an Ubuntu server.

I looked at the log file and found these entries for the LBF editor.

[Mon Dec 03 13:23:38.002240 2018] [:error] [pid 16574] [client 70.184.177.14:51361] PHP Notice:  Undefined index: edit_options in /var/www/html/t/interface/super/edit_layout.php on line 392, referer: https://openemr/interface/super/edit_layout.php
[Mon Dec 03 13:23:38.002246 2018] [:error] [pid 16574] [client 70.184.177.14:51361] PHP Notice:  Undefined index: validation in /var/www/html/t/interface/super/edit_layout.php on line 396, referer: https://openemr/interface/super/edit_layout.php
[Mon Dec 03 13:23:38.005497 2018] [:error] [pid 16574] [client 70.184.177.14:51361] PHP Notice:  Undefined index: action in /var/www/html/t/interface/super/edit_layout.php on line 350, referer: https://openemr/interface/super/edit_layout.php
[Mon Dec 03 13:23:38.005520 2018] [:error] [pid 16574] [client 70.184.177.14:51361] PHP Notice:  Undefined index: edit_options in /var/www/html/t/interface/super/edit_layout.php on line 392, referer: https://openemr/interface/super/edit_layout.php

I did some research on the error message and found this:
https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined
https://wiki.php.net/rfc/isset_ternary

My question is about line 350

 $action = $iter['action']

Changing it to this:

 $action = $iter['action'] ?? '';

Comments suggestion
@robert.down

The not saving had nothing to do with the error messages in the log but I needed to increase the max_input_vars to 4000.

But changing the line 350 got rid of the error message.

An Undefined Index notice should not stop the script for working.

Generally the safest way to manage this kind of notice is to do one of the following

$action = array_key_exists('action', $iter) ? $iter['action'] : 'default value';
// $action will be default value if that key does not exist

or

$action = isset($iter['action']) ? $iter['action'] : 'default value';
// $action will be default value if that key does not exist

The difference is using isset() will return FALSE if the value of action is NULL whereas array_key_exists() would return TRUE (Because the key does exist, regardless of the value).

Both are correct, just depends on what exactly you’re looking for.

Either way the ternary operator is used like this:

$variable = some_test() ? 'value if some_test() is TRUE' : 'value if some_test() is FALSE';

Ternary operators are great and help write safer code :slight_smile: