最近在做微信公众号开发,在一个发送图文接口中,需要把数组元素拼接在XML字符串中,本文主要和大家分享一篇基于php双引号中访问数组元素报错的解决方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧,希望能帮助到大家。

foreach ($itemArr as $key => $value){

$items .= "

";

}

结果竟报如下错误信息:Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in D:\hhp\wamp\www\weixin\wx_sample.php on line 146

从错误信息看是单引号的问题,果断去掉之后就没报错了。然而我就纳闷了,引用下标为字符串的数组元素难道不该加引号吗?到php官方手册去查了关于数组的描述,有一段是这样的:$arr = array('fruit' => 'apple', 'veggie' => 'carrot');

// This will not work, and will result in a parse error, such as:

// Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING'

// This of course applies to using superglobals in strings as well

print "Hello $arr['fruit']";

print "Hello $_GET['foo']";

这里给出了两种错误的写法,当一个普通数组变量或超全局数组变量包含在双引号中时,引用索引为字符串的数组元素,索引字符串不应该再添加单引号。那正确的写法是怎样的呢?于是我继续查找官方手册,找到如下说法:$arr = array('fruit' => 'apple', 'veggie' => 'carrot');

// This defines a constant to demonstrate what's going on. The value 'veggie'

// is assigned to a constant named fruit.

define('fruit', 'veggie');

// The following is okay, as it's inside a string. Constants are not looked for// within strings, so no E_NOTICE occurs hereprint "Hello $arr[fruit]"; // Hello apple// With one exception: braces surrounding arrays within strings allows constants// to be interpretedprint "Hello {$arr[fruit]}"; // Hello carrotprint "Hello {$arr['fruit']}"; // Hello apple

$arr = array('fruit' => 'apple', 'veggie' => 'carrot');

// This defines a constant to demonstrate what's going on. The value 'veggie'

// is assigned to a constant named fruit.

define('fruit', 'veggie');

// The following is okay, as it's inside a string. Constants are not looked for

// within strings, so no E_NOTICE occurs here

print "Hello $arr[fruit]"; // Hello apple

// With one exception: braces surrounding arrays within strings allows constants

// to be interpreted

print "Hello {$arr[fruit]}"; // Hello carrot

print "Hello {$arr['fruit']}"; // Hello apple

这里给出了三种正确的写法:

第一种写法索引字符串不添加任何引号,此时表示获取索引为字符串fruit的数组元素,输出apple。

第二种写法索引字符串也没有添加任何引号,同时将数组变量用一对花括号{ }给包了起来,此时fruit实际上表示一个常量,而不是一个字符串,因此表示获取索引为fruit常量值的数组元素,常量fruit的值是veggie,所以输出carrot。

第三种写法是引用字符串不但添加了单引号,同时也将数组变量用一对花括号{ }给包了起来,此时表示获取索引为字符串fruit的数组元素,输出apple。

后来我继续查找,发现这样一段代码:// Incorrect. This works but also throws a PHP error of level E_NOTICE because

// of an undefined constant named fruit

//

// Notice: Use of undefined constant fruit - assumed 'fruit' in...

print $arr[fruit]; // apple

print $arr['fruit']; // apple// This defines a constant to demonstrate what's going on. The value 'veggie'// is assigned to a constant named fruit.define('fruit', 'veggie');// Notice the difference nowprint $arr[fruit]; // carrot

print $arr['fruit']; // apple

在正常情况下,数组变量没有被双引号包围时,是否给索引字符串加上单引号输出结果都一致时apple,但是当定义一个与索引字符串fruit同名的常量时,未加单引号的索引字符串输出结果就成了carrot,而加上单引号还是apple。

结论:

1. 数组变量未用双引号包括时,

(1) 索引字符串加单引号表示字符串本身

$arr['fruit']

(2)索引字符串未加单引号表示常量,当常量未定义时则解析为字符串,等效于加上单引号。$arr[fruit]

2. 数组变量用双引号包括时,

(1) 索引字符串不加单引号表示字符串本身"$arr[fruit]"

(2) 数组变量加上花括号表示与字符串同名常量"{$arr[fruit]}"

(3) 索引字符串加上单引号且数组变量加上花括号表示字符串本身

"{$arr['fruit']}"

(4) 索引字符串加上单引号且数组变量未加上花括号,为错误写法,报错:Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING'

"$arr['fruit']"

相关推荐:

php访问数组用引号_php双引号中访问数组元素报错如何解决相关推荐

  1. 单引号,双引号,转义

    2011-07-07 11:14 单引号,双引号,转义 最近发现一个问题,是有关引号与转义的,于是就整理了一下这方面的知识,以方便以后温故. 双引号: ·在字串中使用变量 这个功能让你无须使用连接符号 ...

  2. mysql sql语句 引号_关于sql:何时在MySQL中使用单引号,双引号和反引号

    我正在尝试学习编写查询的最佳方法. 我也理解保持一致的重要性. 到现在为止,我已经随机使用单引号,双引号和反引号而没有任何实际想法. 例: $query = 'INSERT INTO table (i ...

  3. Bash中单引号和双引号之间的区别

    本文翻译自:Difference between single and double quotes in Bash 在bash,什么是单引号(之间的差异'' )和双引号( "" ) ...

  4. 字符串双引号表示c语言,c语言中单引号和双引号的区别(顺利解决从字符串中提取IP地址的困惑)...

    c语言中的单引号和双引号可是有很大区别的,使用之前一定要了解他们之间到底有什么不同,下面小编就给大家详细的介绍一下吧,对此还不是很了解的朋友可以过来参考下 问题:从c++文件中将std:string转 ...

  5. 在php中单引号和双引号的区别,php中单引号和双引号有什么区别?

    php字符串中的双引号与单引号区别 php 单引号和双引号的区别: 双引号串中的内容可以被解释而且替换,而单引号串中的内容总被认为是普通字符. 例如: $foo = 2; echo "foo ...

  6. python中单引号,双引号,多引号区别

    先说1双引号与3个双引号的区别,双引号所表示的字符串通常要写成一行  如:  s1 = "hello,world"  如果要写成多行,那么就要使用\ ("连行符" ...

  7. python中单引号和双引号的区别_python中单引号,双引号,多引号区别

    先说1双引号与3个双引号的区别,双引号所表示的字符串通常要写成一行 如: s1 = "hello,world" 如果要写成多行,那么就要使用\ ("连行符")吧 ...

  8. linux 单引号,双引号,反引号

    单引号 目的: 为了保护文字不被转换.除了他本身. 就是说除去单引号外, 在单引号内的所有文字都是原样输出. 1. [root@jszwl161 SP49EP9]# echo '$*><! ...

  9. python单双三引号区别_python中单引号,双引号,多引号区别_python中单双引号

    python中单引号,双引号,多引号区别 先说1双引号与3个双引号的区别,双引号所表示的字符串通常要写成一行 如: s1 = "hello,world" 如果要写成多行,那么就要使 ...

最新文章

  1. “95后”曹原又双叒叕发Nature了!1个月2篇,已经第6篇了……
  2. C/C++ VS java
  3. 将win7电脑变身WiFi热点,让手机、笔记本共享上网
  4. Java Socket编程 - 基于TCP方式的二进制文件传输
  5. UVA-10054 The Necklace (欧拉回路)
  6. Win10专业版系统PyCharm专业版使用WSL(ubuntu20.04 LTS)配置Docker解释器配置环境详细教程
  7. 使用 jQuery Mobile 与 HTML5 开发 Web App —— jQuery Mobile 默认配置与事件基础
  8. 如何使用Behat在Drupal中使用行为驱动的开发
  9. OAuth2.0_JWT令牌介绍_Spring Security OAuth2.0认证授权---springcloud工作笔记147
  10. python return的理解_python 浅析对return的理解
  11. Go语言---面向对象编程
  12. ACL2020 | 词向量性别偏见
  13. 线性回归模型异方差解决方法
  14. 用 dfuse Lifecycle 保证你的交易被推送上链
  15. 手把手教你如何通过OAuth2.0新浪开放平台认证新浪用户-java
  16. sinon spy_Sinon教程:使用嘲弄,间谍和存根进行JavaScript测试
  17. cad注释比例和打印比例不一样_CAD注释比例与打印比例不相等怎么办?
  18. OSI TCP/IP
  19. [翻译] 在 Overleaf 中追踪修订
  20. Qcom 平台 LK 阶段配置 I2C

热门文章

  1. tf.broadcast_to
  2. opencv 转换图像为灰度
  3. tensorflow 加载模型
  4. java floatmath_【Android】解决FloatMath类中方法在API 23以后不存在问题
  5. 机器学习笔记 时间序列预测(最基本的方法【benchmark】)
  6. 强化学习笔记:Q-learning :temporal difference 方法
  7. 机器学习中的数学(2)-线性回归,偏差、方差权衡
  8. 朴素贝叶斯(NaiveBayes)算法总结
  9. Python简明教程
  10. 小白入门深度学习 | 第三篇:30分钟入门深度学习 - TensorFlow版