基于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脚本的10个技巧(8)
Oct 09 PHP
PHP之APC缓存详细介绍 apc模块安装
Jan 13 PHP
PHP创建桌面快捷方式的实例代码
Feb 17 PHP
如何阻止网站被恶意反向代理访问(防网站镜像)
Mar 18 PHP
Codeigniter实现多文件上传并创建多个缩略图
Jun 12 PHP
PHP SPL标准库之接口(Interface)详解
May 11 PHP
PHP实现的简单缓存类
Jul 29 PHP
PHP实现根据图片色界在不同位置加水印的方法
Aug 08 PHP
PHP入门教程之日期与时间操作技巧总结(格式化,验证,获取,转换,计算等)
Sep 11 PHP
PHP使用递归算法无限遍历数组示例
Jan 13 PHP
php 一维数组的循环遍历实现代码
Apr 10 PHP
Windows平台实现PHP连接SQL Server2008的方法
Jul 26 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 fckeditor 调用的函数
2009/06/21 PHP
制作安全性高的PHP网站的几个实用要点
2014/12/30 PHP
fsockopen pfsockopen函数被禁用,SMTP发送邮件不正常的解决方法
2015/09/20 PHP
PHP实现适用于自定义的验证码类
2016/06/15 PHP
php验证身份证号码正确性的函数
2016/07/20 PHP
php中使用websocket详解
2016/09/23 PHP
php数组指针操作详解
2017/02/14 PHP
js location.replace与location.reload的区别
2010/09/08 Javascript
Jquery时间验证和转换工具小例子
2013/07/01 Javascript
多个jquery.datatable共存,checkbox全选异常的快速解决方法
2013/12/10 Javascript
javascript文件中引用依赖的js文件的方法
2014/03/17 Javascript
Angular.js如何从PHP读取后台数据
2016/03/24 Javascript
微信小程序 图片等比例缩放(图片自适应屏幕)
2016/11/16 Javascript
Node.js中多进程模块Cluster的介绍与使用
2017/05/27 Javascript
Vue实现购物车场景下的应用
2017/11/27 Javascript
js实现轮播图效果 z-index实现轮播图
2020/01/17 Javascript
node.js中对Event Loop事件循环的理解与应用实例分析
2020/02/14 Javascript
Python代理抓取并验证使用多线程实现
2013/05/03 Python
python爬虫入门教程之点点美女图片爬虫代码分享
2014/09/02 Python
通过mod_python配置运行在Apache上的Django框架
2015/07/22 Python
python numpy函数中的linspace创建等差数列详解
2017/10/13 Python
使用python存储网页上的图片实例
2018/05/22 Python
python一行sql太长折成多行并且有多个参数的方法
2018/07/19 Python
python 给图像添加透明度(alpha通道)
2020/04/09 Python
Keras模型转成tensorflow的.pb操作
2020/07/06 Python
Django3中的自定义用户模型实例详解
2020/08/23 Python
详解KMP算法以及python如何实现
2020/09/18 Python
HTML5中meta属性的使用方法
2016/02/29 HTML / CSS
职业技术学校毕业生推荐信
2013/12/03 职场文书
高三语文教学反思
2014/01/15 职场文书
小学班主任培训方案
2014/06/04 职场文书
党员公开承诺书2015
2015/01/21 职场文书
复兴之路展览观后感
2015/06/02 职场文书
小学生必读成语故事大全:送给暑假的你们
2019/07/09 职场文书
Python Pandas知识点之缺失值处理详解
2021/05/11 Python
利用python进行数据加载
2021/06/20 Python