Solution to error reporting of accessing array elements in double quotation marks based on php

  • 2021-09-04 23:47:49
  • OfStack

Recently, I am developing WeChat official account. In an interface for sending pictures and texts, I need to splice array elements into XML strings


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 was reported:


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

From the error message, it is a problem of single quotation marks. After being removed decisively, there is no error. However, I wonder, shouldn't array elements referenced as strings be quoted? I went to the official php manual to check the description of arrays, and one paragraph reads like this:


$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']"; 

There are two wrong ways to write it. When an ordinary array variable or a hyper-global array variable is contained in double quotation marks, the array element whose index is a string should be referenced, and the index string should not be added with single quotation marks. What is the correct way to write it? So I continued to search the official manual and found the following statement:


$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

Here are three correct ways to write:

In the first way, the index string is written without any quotation marks, which means that the array element with the index of string fruit is obtained and apple is output.

In the second way, the index string does not add any quotation marks, and the array variables are wrapped in a pair of curly braces {}. At this time, fruit actually represents a constant, not a string, so it means to get the array element whose index is fruit constant value, and the value of constant fruit is veggie, so it outputs carrot.

The third way to write is to not only add single quotation marks to the reference string, but also wrap the array variables with a pair of curly braces {}, which means to get the array elements with the index of string fruit and output apple.

Later, I continued to search and found this 1 piece of code:


// 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 

Under normal circumstances, when the array variable is not enclosed by double quotation marks, whether the index string is quoted in single quotation marks or not will result in apple. However, when a constant with the same name as the index string fruit is defined, the output result of the index string without single quotation marks will become carrot, while the single quotation marks will still be apple.

Conclusions:

1. When an array variable is not included in double quotation marks,

(1) Index string with single quotation marks denotes the string itself


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

(2) The index string without single quotation marks indicates a constant, and when the constant is undefined, it is parsed as a string, which is equivalent to single quotation marks.


$arr[fruit] 

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

(1) The index string does not enclose single quotation marks to indicate the string itself


"$arr[fruit]" 

(2) Array variables with curly braces represent constants with the same name as strings


"{$arr[fruit]}" 

(3) Index strings are in single quotation marks and array variables are in curly braces to denote the strings themselves


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
0

(4) The index string is marked with single quotation marks and the array variable is not marked with curly braces, which is an error. Errors are reported: Parse error: parse error, expecting T_STRING 'or T_VARIABLE' or T_NUM_STRING '


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
1

Attachment: php Manual Array Description URL

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


Related articles: