虽然我使用isset,但我得到一个未定义的索引错误

问题描述 投票:0回答:3

我知道它要求太多时间。但isset功能并没有解决我的问题。

$get = (isset($this->settings[$set['id']])) ? $this->settings[$set['id']] : '';

注意:未定义的索引:第419行的\ public_html \ settings.php中的id

php isset undefined-index
3个回答
1
投票

也许,$set['id']必须检查,像这样:

$set_ = isset($set['id']) ? $set['id'] : '';
$value = isset($this->settings[$set_]) ? $this->settings[$set['id']] : '';

2
投票

在将变量用作参数之前,请尝试检查变量是否已设置。

$get = isset( $set['id']) ? $this->settings[$set['id']] : '';

1
投票

我只是将它添加到isset调用

$get = isset( $set['id'],$this->settings[$set['id']]) ? $this->settings[$set['id']] : '';

您可以在isset中使用多个参数。这大致相当于这样做:

$get = isset($set['id']) && isset($this->settings[$set['id']]) ? $this->settings[$set['id']] : '';

使用此代码可以轻松测试:

$array = ['foo' => 'bar'];
$set = []; //not set
#$set = ['id' => 'foo']; //uncomment to test if set


#using [] to add an element to a string not an array
$get = isset($set['id'],$array[$set['id']]) ? $array[$set['id']] : '';

echo $get;

$set = ['id' => 'foo']输出为bar时,如果你留下那个评论,那么输出是一个空字符串。

Sandbox

© www.soinside.com 2019 - 2024. All rights reserved.