濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > 基于php雙引號(hào)中訪(fǎng)問(wèn)數(shù)組元素報(bào)錯(cuò)的解決方法

基于php雙引號(hào)中訪(fǎng)問(wèn)數(shù)組元素報(bào)錯(cuò)的解決方法

熱門(mén)標(biāo)簽:電話(huà)機(jī)器人危險(xiǎn)嗎 南寧高頻外呼回?fù)芟到y(tǒng)哪家好 江蘇外呼電銷(xiāo)機(jī)器人報(bào)價(jià) 專(zhuān)業(yè)電話(huà)機(jī)器人批發(fā)商 長(zhǎng)沙crm外呼系統(tǒng)業(yè)務(wù) 深圳外呼系統(tǒng)收費(fèi) 400電話(huà)申請(qǐng)方法收費(fèi) 離石地圖標(biāo)注 400電話(huà)辦理福州市

最近在做微信公眾號(hào)開(kāi)發(fā),在一個(gè)發(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é)果竟報(bào)如下錯(cuò)誤信息:

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

從錯(cuò)誤信息看是單引號(hào)的問(wèn)題,果斷去掉之后就沒(méi)報(bào)錯(cuò)了。然而我就納悶了,引用下標(biāo)為字符串的數(shù)組元素難道不該加引號(hào)嗎?到php官方手冊(cè)去查了關(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']"; 

這里給出了兩種錯(cuò)誤的寫(xiě)法,當(dāng)一個(gè)普通數(shù)組變量或超全局?jǐn)?shù)組變量包含在雙引號(hào)中時(shí),引用索引為字符串的數(shù)組元素,索引字符串不應(yīng)該再添加單引號(hào)。那正確的寫(xiě)法是怎樣的呢?于是我繼續(xù)查找官方手冊(cè),找到如下說(shuō)法:

$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

這里給出了三種正確的寫(xiě)法:

第一種寫(xiě)法索引字符串不添加任何引號(hào),此時(shí)表示獲取索引為字符串fruit的數(shù)組元素,輸出apple。

第二種寫(xiě)法索引字符串也沒(méi)有添加任何引號(hào),同時(shí)將數(shù)組變量用一對(duì)花括號(hào){ }給包了起來(lái),此時(shí)fruit實(shí)際上表示一個(gè)常量,而不是一個(gè)字符串,因此表示獲取索引為fruit常量值的數(shù)組元素,常量fruit的值是veggie,所以輸出carrot。

第三種寫(xiě)法是引用字符串不但添加了單引號(hào),同時(shí)也將數(shù)組變量用一對(duì)花括號(hào){ }給包了起來(lái),此時(shí)表示獲取索引為字符串fruit的數(shù)組元素,輸出apple。

后來(lái)我繼續(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ù)組變量沒(méi)有被雙引號(hào)包圍時(shí),是否給索引字符串加上單引號(hào)輸出結(jié)果都一致時(shí)apple,但是當(dāng)定義一個(gè)與索引字符串fruit同名的常量時(shí),未加單引號(hào)的索引字符串輸出結(jié)果就成了carrot,而加上單引號(hào)還是apple。

結(jié)論:

1. 數(shù)組變量未用雙引號(hào)包括時(shí),

(1) 索引字符串加單引號(hào)表示字符串本身

pre name="code" class="php">$arr['fruit'] 

(2)索引字符串未加單引號(hào)表示常量,當(dāng)常量未定義時(shí)則解析為字符串,等效于加上單引號(hào)。

$arr[fruit] 

2. 數(shù)組變量用雙引號(hào)包括時(shí),

(1) 索引字符串不加單引號(hào)表示字符串本身

"$arr[fruit]" 

(2) 數(shù)組變量加上花括號(hào)表示與字符串同名常量

"{$arr[fruit]}" 

(3) 索引字符串加上單引號(hào)且數(shù)組變量加上花括號(hào)表示字符串本身

pre name="code" class="php">pre name="code" class="php">"{$arr['fruit']}" 

(4) 索引字符串加上單引號(hào)且數(shù)組變量未加上花括號(hào),為錯(cuò)誤寫(xiě)法,報(bào)錯(cuò):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手冊(cè)數(shù)組說(shuō)明URL

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

以上這篇基于php雙引號(hào)中訪(fǎng)問(wèn)數(shù)組元素報(bào)錯(cuò)的解決方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • PHP數(shù)組式訪(fǎng)問(wèn)接口ArrayAccess用法分析
  • php訪(fǎng)問(wèn)數(shù)組最后一個(gè)元素的函數(shù)end()用法
  • PHP 的ArrayAccess接口 像數(shù)組一樣來(lái)訪(fǎng)問(wèn)你的PHP對(duì)象
  • PHP如何使用array_unshift()在數(shù)組開(kāi)頭插入元素
  • PHP數(shù)組Key強(qiáng)制類(lèi)型轉(zhuǎn)換實(shí)現(xiàn)原理解析
  • PHP讀取遠(yuǎn)程txt文檔到數(shù)組并實(shí)現(xiàn)遍歷
  • PHP基于array_unique實(shí)現(xiàn)二維數(shù)組去重
  • PHP二維數(shù)組分頁(yè)2種實(shí)現(xiàn)方法解析
  • PHP數(shù)組訪(fǎng)問(wèn)常用方法解析

標(biāo)簽:濱州 白酒營(yíng)銷(xiāo) 曲靖 南昌 太原 南京 株洲 興安盟

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《基于php雙引號(hào)中訪(fǎng)問(wèn)數(shù)組元素報(bào)錯(cuò)的解決方法》,本文關(guān)鍵詞  基于,php,雙,引號(hào),中,訪(fǎng)問(wèn),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《基于php雙引號(hào)中訪(fǎng)問(wèn)數(shù)組元素報(bào)錯(cuò)的解決方法》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于基于php雙引號(hào)中訪(fǎng)問(wèn)數(shù)組元素報(bào)錯(cuò)的解決方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    离岛区| 游戏| 诏安县| 榕江县| 龙门县| 永川市| 丘北县| 怀化市| 龙游县| 蒲城县| 临洮县| 陆丰市| 宽城| 富顺县| 安宁市| 江陵县| 康马县| 凤城市| 蛟河市| 卢氏县| 西华县| 涟源市| 利辛县| 合山市| 洞口县| 班玛县| 正安县| 桑日县| 福贡县| 青州市| 东明县| 通化县| 万宁市| 樟树市| 清河县| 陕西省| 葫芦岛市| 瑞丽市| 新宾| 河津市| 嘉义县|