您好,欢迎访问一九零五行业门户网

php双引号中访问数组元素报错如何解决

最近在做微信公众号开发,在一个发送图文接口中,需要把数组元素拼接在xml字符串中,本文主要和大家分享一篇基于php双引号中访问数组元素报错的解决方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧,希望能帮助到大家。
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返回由数组元素组合成的字符串函数implode()
四种php删除数组元素方法实例详解
php多维数组元素操作类的方法
以上就是php双引号中访问数组元素报错如何解决的详细内容。
其它类似信息

推荐信息