登录  /  注册

php双引号中访问数组元素报错如何处理

php中世界最好的语言
发布: 2018-05-19 11:41:50
原创
1726人浏览过

这次给大家带来php双引号中访问数组元素报错如何处理,php双引号中访问数组元素报错处理的注意事项有哪些,下面就是实战案例,一起来看一下。

foreach ($itemArr as $key => $value){ 
  $items .= "<item> 
  <title></title>  
  <description></description> 
  <picurl></picurl> 
  <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' =&gt; 'apple', 'veggie' =&gt; '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' =&gt; 'apple', 'veggie' =&gt; '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' =&gt; 'apple', 'veggie' =&gt; '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 class="brush:php;toolbar:false">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 class="brush:php;toolbar:false">$arr['fruit']
登录后复制

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

$arr[fruit]
登录后复制

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

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

"$arr[fruit]"
登录后复制

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

"{$arr[fruit]}"
登录后复制

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

<pre class="brush:php;toolbar:false"><pre class="brush:php;toolbar:false">"{$arr['fruit']}"
登录后复制

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

<pre class="brush:php;toolbar:false"><pre class="brush:php;toolbar:false">"$arr['fruit']"
登录后复制

附:php手册数组说明URL

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

php实现解析xml并生成sql语句步骤详解

PHP实现QQ登录步骤详解

以上就是php双引号中访问数组元素报错如何处理的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号