1. Simple syntax rules (use curly braces to define variable names for all versions of PHP):
Copy CodeThe code is as follows:
$a = ' flower ';
echo "She received some $as";//invalid; the letter S will be considered as a valid variable name, but the variable here is $ A
echo "She received some ${a}s";//Valid
echo "She received some {$a}s";//valid; Recommended Use method
What we want to express is "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?
Copy CodeThe code is as follows:
echo "She received some $a". S ";
echo "She received some". $a. " S ";//These two habitual formulations should not have curly braces in 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.
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., used with php4+):
Copy CodeThe code is as follows:
echo "valid notation: {$arr [4][3]}";//valid; defining multidimensional arrays
echo "valid notation: {$arr [' foo '][3]}";//valid; When using multidimensional arrays in strings, be sure to enclose them in parentheses
echo "valid notation: {$this->width}00";//effective; if not defined, it becomes $this->width00
echo "valid notation: {$this->value[3]->name}";//valid; This example demonstrates defining chained calls
echo "Valid notation: $name: {${$name}}";//valid; This example demonstrates that the effect is actually a mutable variable
echo "valid notation: {${getname ()}}";//valid; This example demonstrates the return value of a function as a variable name
echo "valid issued: {${$this->getname ()}}";//valid; This example demonstrates the return value of a function as a variable name
Note that 1:echo "This write is valid: {GetName ()}"; The output is: ' Is this write valid:
{getName ()} '. Because it does not contain $, the curly braces are not used as a delimiter
Note 2:echo "This is valid: {$arr [foo][3]}"; Let's start with an experiment before answering this question:
Copy CodeThe code is as follows:
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, then we write it like this?
echo $arr [d];
This error has occurred:
Notice:use of undefined constant d-assumed ' d '
Note: An undefined constant, d, may be used for the ' d '
So if we change the code like this,
Copy CodeThe code is as follows:
Error_reporting (E_all);
$arr = Array (' A ', ' B ', ' C ', ' d ' = ' e ');
Define (' F ', ' d ');
Echo $arr [F];
We found no problem with this time. You can see that the index of the array in the string does not have a problem, but if the notation does not appear in the string, the parsing of {$arr [foo][3]} in the string is parsed 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 go ahead and revise the previous code.
Copy CodeThe code is as follows:
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. Of course, if you are not my student then I can not control so much ...
Note 3:
Copy CodeThe code is as follows:
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 statements
Copy CodeThe code is as follows:
Example one:
$SQL 1 = "SELECT * from table where id={$_get[' id '}";//Example two:
$SQL 2 = "SELECT * from table where id={$this->id}";
OK, curly braces we've got to play here.
http://www.bkjia.com/PHPjc/324598.html www.bkjia.com true http://www.bkjia.com/PHPjc/324598.html techarticle 1. Simple syntax rules (use curly braces to define variable names for all versions of PHP): Copy code as follows: $a = ' flower '; echo "She received some $as";//invalid; the letter S will be considered as ...