久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

基于php雙引號中訪問數組元素報錯的解決方法

下面小編就為大家分享一篇基于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'] 
【網站聲明】本站除付費源碼經過測試外,其他素材未做測試,不保證完整性,網站上部分源碼僅限學習交流,請勿用于商業用途。如損害你的權益請聯系客服QQ:2655101040 給予處理,謝謝支持。

相關文檔推薦

這篇文章主要介紹了PHP有序表查找之插值查找算法,簡單分析了插值查找算法的概念、原理并結合實例形式分析了php實現針對有序表插值查找的相關操作技巧,需要的朋友可以參考下
下面小編就為大家分享一篇ThinkPHP整合datatables實現服務端分頁的示例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
下面小編就為大家分享一篇PHP實現APP微信支付的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
這篇文章主要介紹了PHP實現的多維數組排序算法,結合實例形式對比分析了php針對多維數組及帶有鍵名的多維數組進行排序相關操作技巧與注意事項,需要的朋友可以參考下
這篇文章主要為大家詳細介紹了php結合ajaxuploadfile實現無刷新文件上傳功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本篇文章給大家詳細介紹了PHP開發接口使用RSA進行加密解密方法,對此有興趣的朋友可以學習下。
主站蜘蛛池模板: 国产区免费视频 | 99免费在线视频 | 精品亚洲国产成av人片传媒 | 亚洲精品乱码久久久久久久久久 | 中文亚洲视频 | 国产女人与拘做受视频 | 精品久久久久久久久久久久 | 日韩视频专区 | 激情久久av一区av二区av三区 | 中文字幕精品一区久久久久 | 天堂色| 一级黄色片一级黄色片 | 成人国产精品久久 | 精品一区二区久久久久久久网站 | 2018国产大陆天天弄 | 久久久久久久久国产成人免费 | 黄色永久免费 | 国产精品久久久久久久久久尿 | 懂色中文一区二区三区在线视频 | 一二三四在线视频观看社区 | 久久一起草 | 日韩成人在线观看 | 久久久久久综合 | 日韩波多野结衣 | 午夜精品福利视频 | 亚洲天堂中文字幕 | 国产91丝袜在线18 | 麻豆av在线 | 99视频免费在线观看 | 一区免费观看 | 午夜精品一区二区三区在线视频 | 色网站在线 | 久久久久久亚洲精品 | 成人欧美一区二区三区在线播放 | 国产成人综合一区二区三区 | 国产成人免费视频网站高清观看视频 | 免费观看色 | 色橹橹欧美在线观看视频高清 | 成人免费视频 | 久久久久国产精品人 | 日韩一区二区在线视频 |