首頁  >  文章  >  後端開發  >  基於php雙引號中存取數組元素報錯的解決方法php技巧

基於php雙引號中存取數組元素報錯的解決方法php技巧

jacklove
jacklove原創
2018-06-28 17:39:031370瀏覽

下面小編就為大家分享一篇基於php雙引號中存取陣列元素報錯的解決方法,具有很好的參考價值,希望對大家有幫助。一起跟著小編過來看看吧

最近在做微信公眾號開發,在一個發送圖文介面中,需要把陣列元素拼接在XML字串中

foreach ($itemArr as $key => $value){ 
  $items .= "<item> 
  <Title><![CDATA[$value[&#39;title&#39;]]]></Title>  
  <Description><![CDATA[[$value[&#39;description&#39;]]]></Description> 
  <PicUrl><![CDATA[$value[&#39;picUrl&#39;]]]></PicUrl> 
  <Url><![CDATA[$value[&#39;url&#39;]]]></Url> 
  </item>"; 
}

結果竟報如下錯誤訊息:

#
Parse error: syntax error, unexpected &#39;&#39; (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(&#39;fruit&#39; => &#39;apple&#39;, &#39;veggie&#39; => &#39;carrot&#39;); 
// This will not work, and will result in a parse error, such as: 
// Parse error: parse error, expecting T_STRING&#39; or T_VARIABLE&#39; or T_NUM_STRING&#39; 
// This of course applies to using superglobals in strings as well 
print "Hello $arr[&#39;fruit&#39;]"; 
print "Hello $_GET[&#39;foo&#39;]";

這裡給了兩種錯誤的寫法,當一個普通數組變數或超全域數組變數包含在雙引號中時,引用索引為字串的數組元素,索引字串不應該再添加單引號。那正確的寫法是怎麼樣的呢?於是我繼續找官方手冊,找到如下說法:

$arr = array(&#39;fruit&#39; => &#39;apple&#39;, &#39;veggie&#39; => &#39;carrot&#39;);

// This defines a constant to demonstrate what&#39;s going on. The value &#39;veggie&#39;
// is assigned to a constant named fruit.
define(&#39;fruit&#39;, &#39;veggie&#39;);

// The following is okay, as it&#39;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[&#39;fruit&#39;]}"; // Hello apple

$arr = array(&#39;fruit&#39; => &#39;apple&#39;, &#39;veggie&#39; => &#39;carrot&#39;);

// This defines a constant to demonstrate what&#39;s going on. The value &#39;veggie&#39;
// is assigned to a constant named fruit.
define(&#39;fruit&#39;, &#39;veggie&#39;);

// The following is okay, as it&#39;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[&#39;fruit&#39;]}"; // 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 &#39;fruit&#39; in... 
print $arr[fruit];  // apple 
<pre name="code" class="php">print $arr[&#39;fruit&#39;]; // apple

// This defines a constant to demonstrate what&#39;s going on. The value &#39;veggie&#39;// is assigned to a constant named fruit.define(&#39;fruit&#39;, &#39;veggie&#39;);// Notice the difference nowprint $arr[fruit]; // carrot

print $arr[&#39;fruit&#39;]; // apple

在正常情況下,數組變數沒有被雙引號包圍時,是否給索引字串加上單引號輸出結果都一致時apple,但是當定義一個與索引字串fruit同名的常數時,未加單引號的索引字串輸出結果就成了carrot,而加上單引號還是apple。

結論:

#1. 陣列變數未以雙引號包含時,

(1) 索引字串加上單引號表示字串本身

<pre name="code" class="php">$arr[&#39;fruit&#39;]

(2)索引字串未加單引號表示常數,當常數未定義時則解析為字串,等效於加上單引號。

$arr[fruit]

2. 當陣列變數以雙引號包含時,

(1)索引字串不加單引號表示字串本身

"$arr[fruit]"

#(2) 陣列變數加上花括號表示與字串同名常數

"{$arr[fruit]}"

(3) 索引字串加上單引號且陣列變數加上花括號表示字串本身

<pre name="code" class="php"><pre name="code" class="php">"{$arr[&#39;fruit&#39;]}"

(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[&#39;fruit&#39;]"

附:php手冊陣列說明URL

http://php.net/manual/zh/language.types.array. php

以上這篇基於php雙引號中訪問數組元素報錯的解決方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持php中文網。

您可能感興趣的文章:

php 刪除一維數組中某一個值元素的操作方法php技巧

PHP實作對圖片的反色處理功能php技巧

#php透過pecl方式安裝擴充功能的實例講解php技巧

以上是基於php雙引號中存取數組元素報錯的解決方法php技巧的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn