PHP double quotes to access array elements how to handle errors

Source: Internet
Author: User
Tags cdata parse error php error
This time to bring you the PHP double quotes in the access array element error How to deal with, PHP double quotes in the array elements to access the error handling of the attention of the matter, the following is the actual case, take a look.

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> ';}

As a result, the following error message is reported:

Parse error:syntax Error, unexpected "(T_encapsed_and_whitespace), expecting identifier (t_string) or variable (t_varia BLE) or number (t_num_string) in D:\hhp\wamp\www\weixin\wx_sample.php on line 146

From the error message is a single quotation mark problem, decisively removed after the error. I wonder, however, that an array element referencing a string should not be quoted? To the official PHP manual to check the description of the array, one of the following:

$arr = Array (' fruit ' = ' apple ', ' veggie ' = ' carrot '); This won't work, and would result in a parse error, such as://Parse Error:parse error, expecting t_string ' or T_va Riable ' or t_num_string '//This of the course applies to using superglobals in strings as well print "Hello $arr [' Fruit ']"; Print "Hello $_get[' foo ']";

Here are two wrong ways to write, when an ordinary array variable or a super global array variable is enclosed in double quotation marks, referencing an array element that is indexed as a string, the index string should not add single quotation marks. What is the correct wording? So I went on to find the Official handbook and found the following:

$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 INS IDE a string.   Constants is 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 "H  Ello {$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 INS IDE a string.   Constants is 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

Here are three ways to write the correct wording:

The first type of index string does not add any quotation marks, which means getting an array element indexed as a string fruit, outputting apple.

The second type of index string also does not add any quotation marks, and the array variable is wrapped with a pair of curly braces {}, when fruit actually represents a constant, not a string, so it represents an array element that is indexed to the fruit constant value. The value of the constant fruit is veggie, so the output is carrot.

The Third Way to do this is to add single quotes to the reference string, and to wrap the array variable with a pair of curly braces {}, which means getting an array element indexed as a string fruit, and outputting apple.

Later I went on to find a piece of code that looked like this:

Incorrect.  This works but also throws a PHP error of level E_notice because//for 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 $ar R[fruit]; Carrotprint $arr [' Fruit ']; Apple

Under normal circumstances, if the array variable is not surrounded by double quotation marks, whether the index string plus the single quote output is consistent with Apple, but when defining a constant with the same name as the index string fruit, the output of the unquoted index string is carrot, Plus single quotes or apple.

Conclusion:

1. When the array variable is not enclosed in double quotation marks,

(1) Index string plus single quotation marks for the string itself

<pre name= "code" class= "PHP" > $arr [' Fruit ']

(2) The index string does not enclose a constant in single quotation marks, and resolves to a string when the constant is undefined, equivalent to adding single quotation marks.

$arr [Fruit]

2. When the array variables are enclosed in double quotation marks,

(1) The index string does not enclose a single quotation mark to denote the string itself

"$arr [Fruit]"

(2) array variable plus curly braces to indicate constant with the same name as String

"{$arr [fruit]}"

(3) Index string plus single quotation mark and array variable plus curly braces for the string itself

<pre name= "code" class= "PHP" ><pre name= "code" class= "PHP" > "{$arr [' Fruit ']}"

(4) Index string plus single quotation mark and array variable without curly brace, error: 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 ']"

Attached: PHP manual array Description URL

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

PHP implementation parsing XML and generating SQL statement steps in detail

PHP implementation of QQ login steps in detail

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.