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

 更新时间:2018年02月01日 10:30:42   投稿:jingxian  
下面小编就为大家分享一篇基于php双引号中访问数组元素报错的解决方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

最近在做微信公众号开发,在一个发送图文接口中,需要把数组元素拼接在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无限极分类实现方法分析

    这篇文章主要介绍了php无限极分类实现方法,结合实例形式分析了两种无限极分类的具体实现技巧与相关注意事项,需要的朋友可以参考下
    2019-07-07
  • 深入php处理整数函数的详解

    深入php处理整数函数的详解

    本篇文章是对php处理整数函数进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • php.ini 中文版 (PHP7,PHP8) 金步国

    php.ini 中文版 (PHP7,PHP8) 金步国

    php.ini 中文版 (PHP7,PHP8),比较适合使用php7、php8的朋友使用,需要的朋友可以参考下
    2023-09-09
  • php数组指针操作详解

    php数组指针操作详解

    本文主要介绍了php数组指针操作。具有很好的参考价值,下面跟着小编一起来看下吧
    2017-02-02
  • 使用 MySQL 开始 PHP 会话

    使用 MySQL 开始 PHP 会话

    使用 MySQL 开始 PHP 会话...
    2006-12-12
  • PHP json_encode中文乱码解决方法

    PHP json_encode中文乱码解决方法

    这篇文章主要介绍了PHP json_encode中文乱码解决方法,有遇到此类问题的同学,可以参考一下
    2021-04-04
  • 自制PHP框架之设计模式

    自制PHP框架之设计模式

    本文是自制PHP框架的第三篇,我们主要来讲解下PHP的设计模式的使用,本文我们一起来学习PHP中最常用的三种设计模式:单例设计模式、工厂设计模式和观察者设计模式。
    2017-05-05
  • 常见PHP数据库解决方案分析介绍

    常见PHP数据库解决方案分析介绍

    您可以用很多的方式创建PHP数据库设计、数据库访问和基于数据库的 PHP 业务逻辑代码,但最终一般以错误告终。本文说明了数据库设计和访问数据库的PHP代码中出现的常见问题,以及在遇到这些问题时如何修复它们。
    2015-09-09
  • PHP关联数组实现根据元素值删除元素的方法

    PHP关联数组实现根据元素值删除元素的方法

    这篇文章主要介绍了PHP关联数组实现根据元素值删除元素的方法,实例分析了php差集运算函数array_diff及数组遍历对比删除的两种方法,需要的朋友可以参考下
    2015-06-06
  • php 编写安全的代码时容易犯的错误小结

    php 编写安全的代码时容易犯的错误小结

    PHP是个伟大的web开发语言,灵活的语言,但是看到php程序员周而复始的犯的一些错误。我做了下面这个列表,列出了PHP程序员经常犯的10中错误,大多数和安全相关。看看你犯了几种
    2010-05-05

最新评论