In Kanboard, why do I get undefined index for this code whenever I click the button?
[php7:notice] PHP Notice: Trying to access array offset on value of type null in /var/www/.../public_html/app/Helper/LayoutHelper.php on line 90
[php7:notice] PHP Notice: Undefined index: project in /var/www/.../public_html/app/Helper/LayoutHelper.php on line 91
<?= $this->url->link(t('Delete'), 'ContactsItemsController', 'remove', array('item_id' => $item['id'], 'plugin' => 'AddressBook'), true, 'btn btn-red') ?>
I read that isset
is a solution but if using isset
is the solution, where do I place it? I can’t figure it out.
I tried:
<?= $this->url->link(t('Delete'), 'ContactsItemsController', 'remove', array('item_id' => isset($item['id']), 'plugin' => 'AddressBook'), true, 'btn btn-red') ?>
2
Answers
The explanation was great and an answer for isset for other issues I was having so thanks for explaining that.
The fix for this particular problem was because I was using the
project
layout helper instead of theconfig
one in the controller.To use
isset
you basically wrap around section where you use that array (or even simple variable), like:But you use the
<?=
which is shorthand for<?php echo
andecho
command must have an argument, even if its empty string or null, so you have to pass some value there, which limits your options how to wrap the section withisset
.You could use ternary operator, like:
Or anonymous function executed right away, like:
Note that when you do not explicitly call
return
command, php functions implicitly returnnull
. Also you should use this method for complex cases which you cannot solve by ternary operator, because of the readability, there could be multiple branches which are harder to get when you or someone else return to the code after while.In case you care only about single value and not entire function call or even block, there is shorthand in form of null coalescing operator, like:
Which kinda does the
isset
check internally.It looks way better then ternary operator, don’t you think ?
To the warnings you posted:
Trying to access array offset on value of type null
, this means that the variable$item
is set to null, therefore it is not an array. Code like this produce the same warning:Or the variable
$item
could be undefined, in which case there would be another warningundefined variable $item
.Most likely you are just setting the
$item
variable from database, but you are missing the check after that if you actually get the record.Second warning is irrelevant to the code you posted
Undefined index: project
, this would happen in case like this:So you have variable which is an array, but there is no index/key named as project present in that array.