Similarities and Differences of using braces for PHP Variables

Source: Internet
Author: User
Original article address: www. linuxfly. orgread. php? 522 PHPNotice warning mentioned in the previous log causes Ajax request failure. The reason is that it is still in the switch statement of 13 rows. Therefore, I did some tests on the use of braces in PHP variables, and I also found some points worth attention during usage. I. variables using braces

Original address: http://www.linuxfly.org/read.php? 522 the PHP Notice warning mentioned in the previous log causes the Ajax request to fail. The reason is that it is still in the switch statement of 13 rows. Therefore, I did some tests on the use of braces in PHP variables, and I also found some points worth attention during usage. I. variables using braces

Original address: http://www.linuxfly.org/read.php? 522

The PHP Notice warning mentioned in the previous log causes the Ajax request to fail. The reason is that it is still in the switch statement of 13 rows. Therefore, I did some tests on the use of braces in PHP variables, and I also found some points worth attention during usage.

I. variables using braces
As mentioned in the previous log, the PHP Notice warning is as follows:

switch (${action}.'_'.${child}) {

There is no problem at first glance. I also checked the definition of variables in the PHP Manual: Here.
1. Variable
It can be seen that, like most data, variables use braces in the form of Variable variables ). It is mentioned that:
Reference
In order to use variable variables with arrays, you have to resolve an ambiguity problem. that is, if you write $ a [1] then the parser needs to know if you meant to use $ a [1] as a variable, or if you wanted $ a as the variable and then the [1] index from that variable. the syntax for resolving this ambiguity is: $ {$ a [1]} for the first case and $ {$ a} [1] for the second.

That is to say, in order to use variable variables in the array environment, you need to properly use braces {} to limit the range of variables according to different situations. $ {$ A [1]} and $ {$ a} [1] are completely different:
Reference
$ {$ A [1]}? $ A [1] is a variable;
$ {$ A} [1] $ a is a variable;

2. Define and avoid ambiguity
In fact, this is similar to variable variables. For example, if you use a "." connector to connect a string, it may be like this:

Echo $ str. '_ 2010 ′;

It may be simpler to use braces:

echo "${str}_2010";

It can be seen that if no braces exist, $ str_2010 may be treated as a variable. Of course, such a statement can only be used in double quotation marks. variable replacement is not performed in single quotation marks.

3. A single character in a string variable
For example:

$ Str = '000'; $ str {0} = '1'; echo $ str; // The output is 100.

This is not hard to understand. It is consistent with the function of []. It is a bit similar to the situation where Python regards strings as objects. Therefore, the following statements have the same functions:

$ Str = '000'; $ str [0] = '1'; echo $ str; // also outputs 100

However, these are not what I want to describe. For details, see the following.

Ii. Similarities and Differences between variables using braces
First, open all PHP error messages, that is,/etc/php. ini is:
Reference
Error_reporting? =? E_ALL
Display_errors = On

Then, open the test page, where the code is:

$test='123';echo $test;echo "${test}";echo "{$test}";echo ${test}.'_';echo ${test};

You will see the following results:
Reference
123123123
Notice: Use of undefined constant test-assumed 'test' in/var/www/html/phpcrm/testpages/variables. php on line 6
123 _
Notice: Use of undefined constant test-assumed 'test' in/var/www/html/phpcrm/testpages/variables. php on line 7
123

What does this mean?
1. Acceptable writing
"123123123" in the output result indicates that the echo statements of the first three rows are normal:

echo $test;echo "${test}";echo "{$test}";

2. Not recommended syntax
The following two rows both have a Notice warning, that is, they regard the test variable as a constant, but are only assumed to be a variable for processing. Therefore, to avoid ambiguity and conflict, it is not recommended to write as follows:

echo ${test}.'_';echo ${test};

3. incorrect syntax
According to many materials on the Internet, $ {var} and {$ var} play the same role. However, if you add the following sentence:

Echo {$ test };

Then, you will get the following error message:
Reference
Parse error: syntax error, unexpected '{' in/var/www/html/phpcrm/testpages/variables. php on line 8

This is not a Notice warning. It is an error. Due to parsing problems, the program will not run properly.

Iii. Summary
Based on the content of the previous two parts, I believe that braces should be used for variable reference according to the following principles:
Reference
1. The correct format is $ {var;
2. Use it with double quotation marks;
3. Define as needed.

So, I changed the switch line:

switch ("${action}_${child}") {

That is, the Notice warning is no longer displayed.

Original article address: [go] similarities and differences between the use of braces in PHP variables. Thank you for sharing it with the original author.

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.