不需要在PHP命名索引上加上引号吗? [英] Is Putting Quotes on PHP Named Indexes Unnecessary?

查看:79
本文介绍了不需要在PHP命名索引上加上引号吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下内容:

$arr = [
    foo => 'bar',
    bar => 'foo'
];

与:

$arr = [
    'foo' => 'bar',
    'bar' => 'foo'
];

换句话说,是否不需要在命名索引上加上引号?何时才真正需要在字符串索引上加上引号?

In other words, is putting quotes on named indexes unnecessary? When would be the only times when putting quotes on string indexes be really needed?

推荐答案

第一个示例应抛出 NOTICE .如果您不使用引号,则PHP将查找具有该名称的常量.

Your first example should throw a NOTICE. If you do not use quotes then PHP will look for a constant with that name.

php > $myArr = [abc => 'hello'];
PHP Notice:  Use of undefined constant abc - assumed 'abc' in php shell code on line 1
PHP Stack trace:
PHP   1. {main}() php shell code:0

Notice: Use of undefined constant abc - assumed 'abc' in php shell code on line 1

Call Stack:
    9.7779     350840   1. {main}() php shell code:0


我在PHP 7.1.8中运行了此示例,但是在PHP 7.2中运行了此版本已弃用.

这篇关于不需要在PHP命名索引上加上引号吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆