基于php双引号中访问数组元素报错的解决方法


Posted in PHP onFebruary 01, 2018

最近在做微信公众号开发,在一个发送图文接口中,需要把数组元素拼接在XML字符串中

foreach ($itemArr as $key => $value){ 
  $items .= "<item> 
  <Title><![CDATA[$value['title']]]></Title>  
  <Description><![CDATA[[$value['description']]]></Description> 
  <PicUrl><![CDATA[$value['picUrl']]]></PicUrl> 
  <Url><![CDATA[$value['url']]]></Url> 
  </item>"; 
}

结果竟报如下错误信息:

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 
<pre name="code" class="php">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) 索引字符串加单引号表示字符串本身

<pre name="code" class="php">$arr['fruit']

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

$arr[fruit]

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

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

"$arr[fruit]"

(2) 数组变量加上花括号表示与字符串同名常量

"{$arr[fruit]}"

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

<pre name="code" class="php"><pre name="code" class="php">"{$arr['fruit']}"

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

<pre name="code" class="php"><pre name="code" class="php">"$arr['fruit']"

附:php手册数组说明URL

http://php.net/manual/zh/language.types.array.php

以上这篇基于php双引号中访问数组元素报错的解决方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

PHP 相关文章推荐
Mysql的Root密码忘记,查看或修改的解决方法(图文介绍)
Jun 14 PHP
PHP数组排序函数合集 以及它们之间的联系分析
Jun 27 PHP
ThinkPHP CURD方法之data方法详解
Jun 18 PHP
浅析get与post的一些特殊情况
Jul 28 PHP
php采用curl模仿登录人人网发布动态的方法
Nov 07 PHP
php使用Cookie控制访问授权的方法
Jan 21 PHP
php的闭包(Closure)匿名函数初探
Feb 14 PHP
深入浅析Yii admin的权限控制
Aug 31 PHP
理清PHP在Linxu下执行时的文件权限方法
Jun 07 PHP
CentOS7系统搭建LAMP及更新PHP版本操作详解
Mar 26 PHP
TP5框架安全机制实例分析
Apr 05 PHP
PHP文件打开关闭及读写操作示例解析
Aug 06 PHP
PHP运用foreach神奇的转换数组(实例讲解)
Feb 01 #PHP
PHP双向链表定义与用法示例
Jan 31 #PHP
基于PHP实现的多元线性回归模拟曲线算法
Jan 30 #PHP
PHP 记录访客的浏览信息方法
Jan 29 #PHP
laravel ORM 只开启created_at的几种方法总结
Jan 29 #PHP
PHP+Redis 消息队列 实现高并发下注册人数统计的实例
Jan 29 #PHP
PHP 使用二进制保存用户状态的实例
Jan 29 #PHP
You might like
php生成随机颜色的方法
2014/11/13 PHP
php多文件打包下载的实例代码
2017/07/12 PHP
用js得到网页中所有的div的id
2020/10/19 Javascript
JavaScript 中的replace方法说明
2007/04/13 Javascript
纯css+js写的一个简单的tab标签页带样式
2014/01/28 Javascript
IE浏览器不支持getElementsByClassName的解决方法
2014/08/27 Javascript
JavaScript用JQuery呼叫Server端方法示例代码
2014/09/03 Javascript
jquery.idTabs 选项卡使用示例代码
2014/09/03 Javascript
js游戏人物上下左右跑步效果代码分享
2015/08/28 Javascript
详解AngularJS 模态对话框
2016/04/07 Javascript
开启BootStrap学习之旅
2016/05/04 Javascript
JS之相等操作符详解
2016/09/13 Javascript
关于Vue.js一些问题和思考学习笔记(1)
2016/12/02 Javascript
基于jQuery实现的幻灯图片切换
2016/12/02 Javascript
Vue组件通信实践记录(推荐)
2017/08/15 Javascript
原生JS实现移动端web轮播图详解(结合Tween算法造轮子)
2017/09/10 Javascript
利用vue开发一个所谓的数独方法实例
2017/12/21 Javascript
Vue 实现手动刷新组件的方法
2019/02/19 Javascript
微信小程序webview 脚手架使用详解
2019/07/22 Javascript
layui文件上传控件带更改后数据传值的方法
2019/09/23 Javascript
jquery实现聊天机器人
2020/02/08 jQuery
jquery实现手风琴案例
2020/05/04 jQuery
vue中echarts引入中国地图的案例
2020/07/28 Javascript
javascript操作向表格中动态加载数据
2020/08/27 Javascript
[03:09]DOTA2亚洲邀请赛 LGD战队出场宣传片
2015/02/07 DOTA
[01:46]2018完美盛典章节片——坚守
2018/12/17 DOTA
Python端口扫描简单程序
2016/11/10 Python
Python partial函数原理及用法解析
2019/12/11 Python
在python3中实现更新界面
2020/02/21 Python
CK巴西官方网站:Calvin Klein巴西
2019/07/19 全球购物
在C语言中实现抽象数据类型什么方法最好
2014/06/26 面试题
会计专业大学生职业生涯规划范文
2014/01/11 职场文书
化学工程专业求职信
2014/08/10 职场文书
教师节校长致辞
2015/07/31 职场文书
2016年第二十届“母亲节暨幸福工程救助贫困母亲活动日”活动总结
2016/04/06 职场文书
python自动计算图像数据集的RGB均值
2021/06/18 Python