基于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 相关文章推荐
php 连接mysql连接被重置的解决方法
Feb 15 PHP
php获取$_POST同名参数数组的实现介绍
Jun 30 PHP
php检测网页是否被百度收录的函数代码
Oct 09 PHP
php实现ip白名单黑名单功能
Mar 12 PHP
Laravel手动分页实现方法详解
Oct 09 PHP
php发送http请求的常用方法分析
Nov 08 PHP
php使用PDO下exec()函数查询执行后受影响行数的方法
Mar 28 PHP
thinkphp下MySQL数据库读写分离代码剖析
Apr 18 PHP
详谈PHP中public,private,protected,abstract等关键字的用法
Dec 31 PHP
safari下载文件自动加了html后缀问题
Nov 09 PHP
php中访问修饰符的知识点总结
Jan 27 PHP
thinkphp3.2框架中where条件查询用法总结
Aug 13 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
laravel model模型处理之修改查询或修改字段时的类型格式案例
2019/10/17 PHP
JavaScript Event学习第十章 一些可替换的事件对
2010/02/10 Javascript
jquery实现简单的拖拽效果实例兼容所有主流浏览器
2013/06/21 Javascript
利用毫秒减值计算时长的js代码
2013/09/22 Javascript
Jquery Uploadify上传带进度条的简单实例
2014/02/12 Javascript
Bootstrap的图片轮播示例代码
2015/08/31 Javascript
JavaScript encodeURI 和encodeURIComponent
2015/12/04 Javascript
基于javascript实现彩票随机数生成(简单版)
2020/04/17 Javascript
jQuery Validate插件实现表单验证
2016/08/19 Javascript
JS+HTML5 FileReader对象用法示例
2017/04/07 Javascript
React Navigation 使用中遇到的问题小结
2018/05/08 Javascript
node中的cookie的具体使用
2018/09/13 Javascript
vue+express+jwt持久化登录的方法
2019/06/14 Javascript
js+canvas实现五子棋小游戏
2020/08/02 Javascript
Python中模块与包有相同名字的处理方法
2017/05/05 Python
50行Python代码实现人脸检测功能
2018/01/23 Python
python求最大连续子数组的和
2018/07/07 Python
使用python验证代理ip是否可用的实现方法
2018/07/25 Python
Python识别快递条形码及Tesseract-OCR使用详解
2019/07/15 Python
pandas DataFrame行或列的删除方法的实现示例
2019/08/02 Python
基于python实现的百度音乐下载器python pyqt改进版(附代码)
2019/08/05 Python
python实现复制大量文件功能
2019/08/31 Python
python cv2在验证码识别中应用实例解析
2019/12/25 Python
python修改linux中文件(文件夹)的权限属性操作
2020/03/05 Python
降低python版本的操作方法
2020/09/11 Python
香港个人化生活购物网站:Ballyhoo Limited
2016/09/10 全球购物
西班牙品牌鞋子、服装和配饰在线商店:Esdemarca
2021/02/17 全球购物
UNIX文件类型
2013/08/29 面试题
军训感想500字
2014/02/20 职场文书
文化与传播毕业生求职信
2014/03/09 职场文书
海飞丝的广告词
2014/03/20 职场文书
2015年挂职锻炼个人总结
2015/10/22 职场文书
2019年英语版感谢信(8篇)
2019/09/29 职场文书
新手必备之MySQL msi版本下载安装图文详细教程
2021/05/21 MySQL
Python趣味爬虫之用Python实现智慧校园一键评教
2021/05/28 Python
Python中glob库实现文件名的匹配
2021/06/18 Python