英文:
In Kanboard, why do I get undefined index for this code whenever I click the button?
问题
In Kanboard,每当我点击按钮时,为什么在这段代码中我会得到未定义的索引错误?
[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
我读到isset
是一个解决方法,但如果使用isset
是解决方法的话,我应该放在哪里?我搞不清楚。
我尝试过:
<?= $this->url->link(t('Delete'), 'ContactsItemsController', 'remove', array('item_id' => isset($item['id']), 'plugin' => 'AddressBook'), true, 'btn btn-red') ?>
英文:
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') ?>
答案1
得分: 0
要使用 isset
,基本上是包裹您使用数组(甚至是简单变量)的部分,如下所示:
<?php
if(isset($array['key'])){
// ... 可以安全使用 $array['key'] 值的部分 ...
echo $this->url->link(...);
}
但您使用了 <?=
,这是 <?php echo
的速记方式,echo
命令必须有一个参数,即使它是空字符串或 null,所以您必须传递一些值,这限制了您如何使用 isset
包裹该部分。
您可以使用三元运算符,如下所示:
<?= isset($array['key']) ? /*... 安全使用 $array['key'] ...*/ $this->url->link(...) : '' ?>
或者立即执行的匿名函数,如下所示:
<?= (function(){ if(isset($array['key'])){ /*... 安全使用 $array['key'] ...*/ return $this->url->link(...); } })() ?>
请注意,当您未显式调用 return
命令时,php 函数会隐式返回 null
。此方法应用于您无法通过三元运算符解决的复杂情况,因为可读性原因,在一段时间后您或其他人回到代码时,可能存在多个分支,难以理解。
如果您只关心单个值而不是整个函数调用或甚至块,可以使用null 合并运算符的速记方式,如下所示:
array('item_id' => $item['id'] ?? 0)
这在内部执行了 isset
检查。
它看起来比三元运算符好看,不是吗?
array('item_id' => isset($item['id']) ? $item['id'] : 0)
至于您发布的警告:“尝试访问类型为 null 的值的数组偏移量”,这意味着变量 $item
设置为 null,因此它不是数组。像这样的代码会产生相同的警告:
$item = null;
$item['id'];
或者变量 $item
可能未定义,这种情况下会有另一个警告“未定义变量 $item”。
最可能的情况是,您只是从数据库设置了 $item
变量,但在那之后您可能忘记了实际获取记录的检查。
第二个警告与您发布的代码无关,“未定义索引:project”,这将在以下情况发生:
$var = [];
$var['project'];
因此,您有一个变量是数组,但在该数组中没有名为 project 的索引/键。
英文:
To use isset
you basically wrap around section where you use that array (or even simple variable), like:
<?php
if(isset($array['key'])){
//... section where we can safely use the value of $array['key'] ...
echo $this->url->link(...);
}
But you use the <?=
which is shorthand for <?php echo
and echo
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 with isset
.
You could use ternary operator, like:
<?= isset($array['key']) ? /*... safe use of $array['key'] ...*/ $this->url->link(...) : '' ?>
Or anonymous function executed right away, like:
<?= (function(){ if(isset($array['key'])){ /*... safe use of $array['key'] ...*/ return $this->url->link(...); } })() ?>
Note that when you do not explicitly call return
command, php functions implicitly return null
. 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:
array('item_id' => $item['id'] ?? 0)
Which kinda does the isset
check internally.
It looks way better then ternary operator, don't you think ?
array('item_id' => isset($item['id']) ? $item['id'] : 0)
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:
$item = null;
$item['id'];
Or the variable $item
could be undefined, in which case there would be another warning undefined 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:
$var = [];
$var['project'];
So you have variable which is an array, but there is no index/key named as project present in that array.
答案2
得分: 0
解释得很好,对于我遇到的其他问题,isset的答案也很有帮助,所以感谢你解释这一点。
对于这个特定问题的修复是因为我在控制器中使用了project
布局助手,而不是config
布局助手。
/**
* 联系人项目配置页面
*
* @access public
*/
public function config()
{
$this->response->html($this->helper->layout->config('addressBook:config/index', array(
'title' => t('通讯录设置'),
'items' => $this->contactsItemsModel->getAllItems(),
)));
}
英文:
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 the config
one in the controller.
/**
* Contacts items config page
*
* @access public
*/
public function config()
{
$this->response->html($this->helper->layout->config('addressBook:config/index', array(
'title' => t('Address Book Settings'),
'items' => $this->contactsItemsModel->getAllItems(),
)));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论