基于php雙引號中訪問數(shù)組元素報錯的解決方法
最近在做微信公眾號開發(fā),在一個發(fā)送圖文接口中,需要把數(shù)組元素拼接在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>"; }
結(jié)果竟報如下錯誤信息:
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
從錯誤信息看是單引號的問題,果斷去掉之后就沒報錯了。然而我就納悶了,引用下標(biāo)為字符串的數(shù)組元素難道不該加引號嗎?到php官方手冊去查了關(guān)于數(shù)組的描述,有一段是這樣的:
$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']";
這里給出了兩種錯誤的寫法,當(dāng)一個普通數(shù)組變量或超全局數(shù)組變量包含在雙引號中時,引用索引為字符串的數(shù)組元素,索引字符串不應(yīng)該再添加單引號。那正確的寫法是怎樣的呢?于是我繼續(xù)查找官方手冊,找到如下說法:
$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的數(shù)組元素,輸出apple。
第二種寫法索引字符串也沒有添加任何引號,同時將數(shù)組變量用一對花括號{ }給包了起來,此時fruit實際上表示一個常量,而不是一個字符串,因此表示獲取索引為fruit常量值的數(shù)組元素,常量fruit的值是veggie,所以輸出carrot。
第三種寫法是引用字符串不但添加了單引號,同時也將數(shù)組變量用一對花括號{ }給包了起來,此時表示獲取索引為字符串fruit的數(shù)組元素,輸出apple。
后來我繼續(xù)查找,發(fā)現(xiàn)這樣一段代碼:
// 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
在正常情況下,數(shù)組變量沒有被雙引號包圍時,是否給索引字符串加上單引號輸出結(jié)果都一致時apple,但是當(dāng)定義一個與索引字符串fruit同名的常量時,未加單引號的索引字符串輸出結(jié)果就成了carrot,而加上單引號還是apple。
結(jié)論:
1. 數(shù)組變量未用雙引號包括時,
(1) 索引字符串加單引號表示字符串本身
<pre name="code" class="php">$arr['fruit']
(2)索引字符串未加單引號表示常量,當(dāng)常量未定義時則解析為字符串,等效于加上單引號。
$arr[fruit]
2. 數(shù)組變量用雙引號包括時,
(1) 索引字符串不加單引號表示字符串本身
"$arr[fruit]"
(2) 數(shù)組變量加上花括號表示與字符串同名常量
"{$arr[fruit]}"
(3) 索引字符串加上單引號且數(shù)組變量加上花括號表示字符串本身
<pre name="code" class="php"><pre name="code" class="php">"{$arr['fruit']}"
(4) 索引字符串加上單引號且數(shù)組變量未加上花括號,為錯誤寫法,報錯: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手冊數(shù)組說明URL
http://php.net/manual/zh/language.types.array.php
以上這篇基于php雙引號中訪問數(shù)組元素報錯的解決方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
php+jQuery+Ajax實現(xiàn)點贊效果的方法(附源碼下載)
這篇文章主要介紹了php+jQuery+Ajax實現(xiàn)點贊效果的方法,結(jié)合實例形式詳細介紹了php結(jié)合jQuery的ajax無刷新提交實現(xiàn)點贊功能的具體步驟與相關(guān)技巧,需要的朋友可以參考下2015-12-12PHP多維數(shù)組轉(zhuǎn)一維數(shù)組的簡單實現(xiàn)方法
這篇文章主要介紹了PHP多維數(shù)組轉(zhuǎn)一維數(shù)組的簡單實現(xiàn)方法,涉及PHP遞歸操作技巧,簡單實用,需要的朋友可以參考下2015-12-12深思 PHP 數(shù)組遍歷的差異(array_diff 的實現(xiàn))
還是部門無聊的考題,不過這次考的是 PHP 的能力。題目如下: 給你兩個分別有 5000 個元素的數(shù)組,計算他們的差集 -- 說白了也就是用 PHP 和你認為最好的算法實現(xiàn) array_diff 的算法。初次接到這個題目,我發(fā)現(xiàn)這非常的簡單,于是按照以往的經(jīng)驗“隨便”寫了一個:2008-03-03php對二維數(shù)組進行相關(guān)操作(排序、轉(zhuǎn)換、去空白等)
這篇文章主要介紹了php對二維數(shù)組進行相關(guān)操作,包括php對二維數(shù)組排序、轉(zhuǎn)換、去空白,以及去重復(fù)值等,感興趣的小伙伴們可以參考一下2015-11-11瀏覽器關(guān)閉后,能繼續(xù)執(zhí)行的php函數(shù)(ignore_user_abort)
希望關(guān)閉瀏覽器后,程序能繼續(xù)在后臺跑,這種情況下需要用到ignore_user_abort()函數(shù)2012-08-08