Introduction to the role of curly braces in PHP _php

Source: Internet
Author: User
Keywords PHP curly braces
Tags parse error
One, no matter what the program, function name () {}, for () {}, .... Too much, do not say also know what to do with.
Second, the $str {4} After the variable of the string followed by {} curly braces and brackets are the same as the string variable as an array processing.
Three, {$val} This time the brace function is to tell PHP, surrounded by as a variable processing.
Copy CodeThe code is as follows:
$arr =array (0=>123, ' name ' = ' Hello ');
foreach ($array as $k = = $v) {
echo "SELECT * from Blog_blogs where blog_tags like '%{$arr [$k]}% ' ORDER by blog_id '; Add a curly brace just as a marker for the variable
Echo '

foreach ($array as $k = = $v) {
echo "SELECT * from Blog_blogs where blog_tags like '%{{$arr [$k]}}% ' ORDER by blog_id '; Add two curly braces, and the outer layer will be the normal character
Use curly braces to differentiate variables
echo "$arr [' name ']"; Use this sentence to report grammatical errors
echo "{$arr [' name ']}"; This sentence is normal, and the characters in curly braces are treated as variables.
$STR {4} follows the variable of the string with the {} curly braces and the brackets, which treat a string variable as an array
$str = ' ABCDEFG ';
echo $str {4};

{} The role of curly braces in PHP (the meaning of the PHP variable is enclosed in curly braces)

such as: $sql = "INSERT into article (' channel_id ', ' title ', ' Detail ', ' pub_time ') values (' {$cid} ', ' {$title} ', ' {$detail} ', ' {$ Time} '); ";
What does it mean to add {}?
And why do you include the field name?


At least easy to read ~ ~ ~ ' is required by the INSERT INTO statement, because the string will appear in pairs
Add {} Sometimes to prevent the variable name from being linked to the following string
For example
So {$cid}dd=aadd
Do not add the words you see the $ciddd, not become a CIDDD variable ~ ~

The PHP variable is appended with a curly brace {}, which is filled with numbers, which refers to the character of the corresponding ordinal of the PHP variable.
For example:
$str = ' Hello ';
echo $str {0}; Output is H
echo $str {1}; Output to E
If you want to check how much length a string satisfies, consider replacing the strlen function with this curly brace (curly brace) and isset, because Isset is a language structure and strlen is a function, so using isset is more efficient than using strlen.
For example, determine whether a string is less than 5 in length:
if (!isset ($str {5})) is better than if (strlen ($STR) < 5).

Here are some explanations for why:

Indicates that {} is a variable that, when executed, handles the special inclusion method of referencing a variable in a string, so that the. operator is not used, thus reducing the amount of code input.

In fact, the output of the block is equivalent to print "Hello". $arr [' fruit '];

A variable with curly braces
The previous log mentions, PHP notice warning is the following sentence:

Switch (${action}. ' _ '. ${child} ') {
At first glance, there's no problem. I also queried the PHP manual on the definition of variables: here.
1. Case of variable variable
It can be seen that, like most data, variables use curly braces in the case of "mutable variables" (Variable variables). which mentions:

In order to use variable variables with arrays, you have to resolve an ambiguity problem. That's, 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 a s the variable and then the [1], index from that variable. The syntax for resolving this ambiguity are: ${$a [1]} for the first case and ${$a}[1] for the second.
In other words, in order to use mutable variables in an array environment, it is necessary to use curly braces {} to limit the range of variables, depending on the situation. ${$a [1]} is completely different from ${$a}[1]:

${$a [1]} here $a[1] is a variable;
${$a}[1] here $ A is a variable;
2. Delimitation and avoidance of ambiguity
In fact, this situation is similar to mutable variables. For example, if you use the "." Connector, concatenate a string, possibly this way:

echo $str. ' _2010 ';
It may be easier to write in curly braces:

echo "${str}_2010";
It can be seen that if there are no curly braces, the whole $str_2010 may be treated as a variable. Of course, such a notation can only be used in double quotation marks, and the single quotation marks do not perform variable substitution.

3. A single character in a string variable
For example:
Copy CodeThe code is as follows:
$str = ' 000 ';
$str {0}= ' 1 ';
Echo $str; Output is 100

This is not difficult to understand, and the role of brackets [] is consistent, a bit like python in the case of the string as an object. Therefore, the following statements function the same:
Copy CodeThe code is as follows:
$str = ' 000 ';
$str [0]= ' 1 ';
Echo $str; Also output 100

However, these are not what I want to explain, the real want to describe the situation, please see below.

The similarities and differences of the use of curly braces for variables
First, the error message output of PHP all open, that is/etc/php.ini:

error_reporting = E_all
Display_errors = On
Then, open the test page, where the code is:
Copy CodeThe code is as follows:
$test = ' 123 ';
Echo $test;
echo "${test}";
echo "{$test}";
Echo ${test}. ' _';
Echo ${test};

You will see the following results:

Notice:use of undefined constant test-assumed ' test ' in/var/www/html/phpcrm/testpages/variables.php on line 6
Notice:use of undefined constant test-assumed ' test ' in/var/www/html/phpcrm/testpages/variables.php on line 7
What does that mean?
1. Acceptable wording
From the output, "123123123" indicates that the Echo statement for the previous three lines is normal:
Copy CodeThe code is as follows:
Echo $test;
echo "${test}";
echo "{$test}";

2, not recommended wording
The next two lines have notice warnings, that is, the test variable as a constant, only later assumed to be a variable to deal with. Therefore, in order to avoid ambiguity and conflict, it is not recommended to write:
Copy CodeThe code is as follows:
Echo ${test}. ' _';
Echo ${test};

However, there can be a workaround to the wording
Copy CodeThe code is as follows:
echo ${' Test '}. ' _';
echo ${' Test '};

If you write like this, you won't get an error.
3. Incorrect wording
A lot of information on the Internet, ${var} and {$var} function is the same. But, if you add one more sentence:

echo {$test};
Then, you will get the following error message:

Parse error:syntax Error, unexpected ' {' in/var/www/html/phpcrm/testpages/variables.php on line 8
This is not a notice warning, it is a mistake and the program will not function properly because of the parsing problem.

Iii. Summary
In conjunction with the previous two sections, I believe that using curly braces for variable references should follow these guidelines:

1, the correct wording is: ${var} form;
2, with the double quotation marks together use;
3, according to the meaning of the need to express the delimitation.
So, finally, I changed the switch line to:

Switch ("${action}_${child}") {
The notice warning is no longer present.
  • 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: 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.