尽管我使用isset,但出现未定义的索引错误

时间:2019-02-27 19:22:47

标签: php isset undefined-index

我知道它问了太多时间。但是isset函数不能解决我的问题。

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

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

3 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

也许[data-v-f3f3eg9] .my-icon { background: red; } 必须像这样检查:

$set['id']

答案 2 :(得分: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

相关问题