Introduction to the role of square brackets in php _ PHP Tutorial

Source: Internet
Author: User
The role of braces in php is described. 1. no matter what program, functionname () {}, (){},.... There are too many things to be done. 2. $ str {4} follows the variable of the string with braces {}. I. No matter what program, function name () {}, (){}, .... There are too many things to be done.
2. $ str {4} after the variable of the string is treated as an array like the variable.
III. {$ val} at this time, braces are used to tell PHP that the enclosed values should be treated as variables.

The code is as follows:


$ Arr = array (0 => 123, 'name' => '');
Foreach ($ array as $ k => $ v ){
Echo "select * from blog_blogs where blog_tags like '% {$ arr [$ k]} % 'Order by blog_id"; // add a braces that will only be used as a variable identifier
}
Echo'






';
Foreach ($ array as $ k => $ v ){
Echo "select * from blog_blogs where blog_tags like '% {$ arr [$ k]} % 'Order by blog_id"; // add two braces, the outer layer is a common character.
}
// Use braces to differentiate variables
// Echo "$ arr ['name']"; // a syntax error is returned when you use this sentence.
Echo "{$ arr ['name']}"; // This sentence is normal. the characters in braces are processed as variables.
// $ Str {4} after the variable of the string, the variable of the string is treated as an array like the braces ({}).
$ Str = 'abcdefg ';
Echo $ str {4 };



{} Role of braces in php (the meaning of PHP variables in braces)

For example, $ SQL = "insert into article ('Channel _ id', 'title', 'detail ', 'pub _ time') values (' {$ cid }', '{$ title}', '{$ detail}', '{$ time }');";
It seems that you can do it without adding {}. what does adding {} mean?
Why does the field name need to be included in?

========================================================== ======

At least it is easy to read ~~~ ''Is required by the insert into statement, because the strings must appear in pairs.
Adding {} is sometimes used to prevent variable names and strings following them from being connected together.
For example
{$ Cid} dd
If cid = aa
So {$ cid} dd = aadd
If this parameter is not added, you can check the $ ciddd variable ~~

Add a braces {} next to the PHP variable, and fill in a number, which is the character of the corresponding sequence number of the PHP variable.
For example:
$ Str = 'hello ';
Echo $ str {0}; // The output is h.
Echo $ str {1}; // The output is e.
If you want to check whether a string contains a certain length, consider using this braces (curly braces) and isset to replace the strlen function, because isset is a language structure and strlen is a function, therefore, using isset is more efficient than using strlen.
For example, to determine whether the length of a string is less than 5:
If (! Isset ($ str {5}) is better than if (strlen ($ str) <5.


The following comparison explains the cause:

It indicates a variable in {}. during execution, the special methods used to reference variables in strings are handled according to the variable. to reduce the input of the code.

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

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:

The code is as follows:


$ 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:

The code is as follows:


$ 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:

The code is as follows:


$ Test = '000000 ';
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:

The code is as follows:


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:

The code is as follows:


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


However, there can be a different way of writing

The code is as follows:


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


In this case, no error will be reported.
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.

Pipeline name () {}, (){},.... There are too many things to be done. 2. $ str {4} follows the variable of the string with braces ({}) and braces...

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.