Commonly used in PHP curly braces use rules

Source: Internet
Author: User

Transfer from http://www.cnblogs.com/jayleke/archive/2011/11/08/2241609.html

1. Simple syntax rules (use curly braces to define variable names for all versions of PHP): $a = ' flower '; echo "She received some $as"; The letter S will be considered as a valid variable name, but here the variable is a $ A echo "She received some ${a}s"; Effective echo "She received some {$a}s"; Effective; Recommended use method we want to express "she received some flowers", the context of the flower should be in the plural (that is, should be appended with s), but if the variables do not define anything, there will be the first echo case. Obviously we want to output a $ A rather than a $as. So what do we usually do with this output? echo "She received some $a". S "; echo "She received some". $a. " S "; These two habitual writing should not add curly braces to the wording of the simple and clear it? Note: Regardless of whether {it appears before or after, only the curly braces will be considered as delimiting symbols when they are next to each other. Do not add spaces between them, otherwise they will be treated as ordinary curly braces for echo "She received some {$a}s"; The result of the output is: She received some {Flower}s

2. Complex syntactic rules (using curly braces to define expressions, etc., with php4+): echo "valid notation: {$arr [4][3]}"; To define a multidimensional array echo "valid notation: {$arr [' foo '][3]}"; Valid; When you use a multidimensional array in a string, be sure to enclose it in parentheses. echo "valid notation: {$this->width}00"; If not defined, it becomes $this->width00 echo "valid notation: {$this->value[3]->name}"; This example demonstrates the definition of a chained call echo "valid notation: $name: {${$name}}"; This example demonstrates that the effect is actually a variable variable echo "valid notation: {${getname ()}}"; This example shows the return value of the function as the variable name echo "valid issued: {${$this->getname ()}}"; This example demonstrates the function's return value as a variable name Note 1:echo "Is this write valid: {getName ()}"; The output is: ' Is this write valid: {getName ()} '. Because it does not contain $, the curly brackets are not treated as a qualifier. 2:echo "Is this write valid: {$arr [foo][3]}"; Before we answer this question, let's start with an experiment: error_reporting (E_all); $arr = Array (' A ', ' B ', ' C ', ' d ' = ' e '); echo "This is $arr [d]"; We find it is no problem to write this, so what do we write like this? echo $arr [d]; This error has occurred: Notice:use of undefined constant d-assumed ' d ' Note: An undefined constant d is used and may be ' d ' then if we modify the code like so error_reporting (e_a LL); $arr = Array (' A ', ' B ', ' C ', ' d ' = ' e '); Define (' F ', ' d '); Echo $arr [F]; We found no problem with this time. It can be seen that the index of an array in a string is not a problem, but if the notation does not appear in the string, it will be an error, and the solution to {$arr [foo][3]} in the stringParsing is resolved in a non-string manner. So it is wrong to say that an array is only enclosed in curly braces in a string and does not add single quotes to an index. This creates an error because the program interprets the non-single-quoted index as a constant. The correct wording should be: echo "valid notation: {$arr [' foo '][3]}"; A special reminder: echo "This is $arr [d]"; Although this notation can be parsed by the program, it is limited to the case that the array is a one-dimensional array. The rigorous wording should be: echo "This is {$arr [' d ']}"; My students have argued with me on this point, and he said: "Since the previous one can produce results, why must we use the latter one?" So, let's continue to modify the previous code error_reporting (E_all); $arr = Array (' A ', ' B ', ' C ', ' d ' =>array (' e ' = = ' f ')); echo "This is $arr [d][e]"; Can this still be parsed correctly? I just want to tell you that the curly braces are strictly necessary.

Note 3:error_reporting (E_all); $arr = Array (' A ', ' B ', ' C ', ' d '); echo "This is {$arr [2]}"; echo "This is {$arr [' 2 ']}"; Execute the above code. The result is the same, why is it so? I can only tell you that PHP is a weakly typed language, as to what is called a weakly typed language I am not here to say more. Go to Google for yourself. With so much to say, where is the specific application of the advantages of these sentence rules? ----SQL statement//Example one: $SQL 1 = "SELECT * from table where id={$_get[' id '}"; Example two: $SQL 2 = "SELECT * from table where id={$this->id}";

Commonly used in PHP curly braces use rules

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.