基于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+javascript模拟Matrix画面
Oct 09 PHP
php 错误处理经验分享
Oct 11 PHP
php数组函数序列之array_intersect() 返回两个或多个数组的交集数组
Nov 10 PHP
第七章 php自定义函数实现代码
Dec 30 PHP
PHP对象Object的概念 介绍
Jun 14 PHP
让CodeIgniter数据库缓存自动过期的处理的方法
Jun 12 PHP
PHP处理JSON字符串key缺少双引号的解决方法
Sep 16 PHP
自定义session存储机制避免会话保持问题
Oct 08 PHP
一款简单实用的php操作mysql数据库类
Dec 08 PHP
简单了解将WordPress中的工具栏移到底部的小技巧
Dec 31 PHP
yii2超好用的日期组件和时间组件
May 05 PHP
基于PHP实现短信验证码接口(容联运通讯)
Sep 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/06/01 PHP
ThinkPHP3.1新特性之查询条件预处理简介
2014/06/19 PHP
php中array_multisort对多维数组排序的方法
2020/06/21 PHP
CodeIgniter基于Email类发邮件的方法
2016/03/29 PHP
JS函数验证总结(方便js客户端输入验证)
2010/10/29 Javascript
jqTransform form表单美化插件使用方法
2012/07/05 Javascript
JS删除数组元素的函数介绍
2013/03/27 Javascript
让网页跳转到指定位置的jquery代码非书签
2013/09/06 Javascript
jquery选择器-根据多个属性选择示例代码
2013/10/21 Javascript
JS中完美兼容各大浏览器的scrolltop方法
2015/04/17 Javascript
基于jQuery实现仿51job城市选择功能实例代码
2016/03/02 Javascript
jquery树形菜单效果的简单实例
2016/06/06 Javascript
JavaScript Ajax编程 应用篇
2016/07/02 Javascript
利用Angularjs和Bootstrap前端开发案例实战
2016/08/27 Javascript
基于原生js淡入淡出函数封装(兼容IE)
2016/10/20 Javascript
Bootstrap实现导航栏的2种方式
2016/11/28 Javascript
简单实现js选项卡切换效果
2017/02/09 Javascript
JS实现生成由字母与数字组合的随机字符串功能详解
2018/05/25 Javascript
详解如何在Angular优雅编写HTTP请求
2018/12/05 Javascript
Vue+webpack实现懒加载过程解析
2020/02/17 Javascript
Python访问MySQL封装的常用类实例
2014/11/11 Python
Pandas实现数据类型转换的一些小技巧汇总
2018/05/07 Python
Python中偏函数用法示例
2018/06/07 Python
python 将print输出的内容保存到txt文件中
2018/07/17 Python
浅谈tensorflow中dataset.shuffle和dataset.batch dataset.repeat注意点
2020/06/08 Python
Mankind美国/加拿大:英国领先的男士美容护发用品公司
2018/12/05 全球购物
Nice Kicks网上商店:ShopNiceKicks.com
2018/12/25 全球购物
物流管理专业大学生自荐信
2013/10/04 职场文书
经贸日语专业个人求职信
2013/12/13 职场文书
80后职场人的职业生涯规划
2014/03/08 职场文书
中国梦团日活动总结
2014/07/07 职场文书
大学生推广普通话演讲稿
2014/09/21 职场文书
幼儿园重阳节活动总结
2015/05/05 职场文书
银行安全保卫工作总结
2015/08/10 职场文书
听课评课活动心得体会
2016/01/15 职场文书
前端JavaScript大管家 package.json
2021/11/02 Javascript