php - 捕获数组中 undefined index 并创建它

标签 php arrays

<h1><?php echo $GLOBALS['translate']['About'] ?></h1>
Notice: Undefined index: About in page.html on line 19

是否可以“捕获”一个 undefined index 以便我可以创建它(数据库查找)并从我的函数中返回它然后执行回显?

最佳答案

检查值是否已分配的最简单方法是使用 isset 方法:

if(!isset($GLOBALS['translate']['About'])) {
    $GLOBALS['translate']['About'] = "Assigned";
}
echo $GLOBALS['translate']['About'];

关于php - 捕获数组中 undefined index 并创建它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3298381/

相关文章:

php - 带有键的数组的 Twig for 循环

javascript - 从 JS 数组中删除指定索引处的对象

PHP MySQL 可能存在语法错误

php - 需要帮助将 mySQL 中的 php 检索列中的第一个 <select> 选项设为空白?

php 将 string 转换为 int 没有给出正确的值

python - 对部分 "sparse"numpy 数组的高效数学运算

ios - 如何在 Xcode 中实现图像数组?

java - 从 php 启动 .bat 出现错误 'java' 无法识别

php - 如何使用 PHP 和 SQLite 3 检查 select 查询是否返回值或为空

c - 在 C 中返回多维字符数组