国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - 基于php雙引號中訪問數組元素報錯的解決方法

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

2019-10-23 13:57jingxian 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'] 

(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雙引號中訪問數組元素報錯的解決方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 久久99国产精品久久99大师 | 日韩精品在线观看视频 | 欧美一区二区在线视频 | 成人av观看 | 国产成人网| 亚洲日本中文字幕 | 久久久亚洲精品中文字幕 | 精品伊人久久 | 一区二区三区 在线 | 天天干天天看天天操 | 中文字幕久久久 | av片在线播放 | 久久久久久亚洲av毛片大全 | 久久久精品456亚洲影院 | 国产精品无码永久免费888 | 狠狠天天| 免费午夜视频 | 日韩中文字幕在线观看 | 亚洲高清在线 | 美女爽到呻吟久久久久 | 国内外成人在线视频 | 成人羞羞视频在线观看免费 | 国产精品视频在线观看 | 国产成人精品久久二区二区91 | 男女xx网站 | 国产日韩欧美在线观看 | 久久一二三四 | 国产成年人电影在线观看 | 成人午夜天堂 | 日韩中文在线 | 国产偷久久9977 | 黄色毛片在线视频 | 中文字幕一区二区三区不卡 | 精品国产乱码久久久久久牛牛 | 久久国产区| 2020国产在线 | 国产亚洲精品美女久久久久久久久久 | 一区二区三区视频在线观看 | 一大道一二三区不卡 | 欧美一区二区三区视频在线观看 | 欧美一级全黄 |