尽管我使用ISSET,但我会遇到一个未定义的索引错误



我知道它要求太多时间。但是isset功能无法解决我的问题。

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

注意:未定义的索引:IN public_html settings.php in Line 419

尝试在将变量作为参数之前设置是否设置。

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

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

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

我只需将其添加到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

沙盒

相关内容