1. Simple syntax rules (use curly braces to define variable names for all versions of PHP):
Copy Code code as follows:
$a = ' flower ';
echo "She received some $as";/invalid; the letter S is used as an element of a valid variable name, but the variable here is $a
echo "She received some ${a}s";/effective
echo "She received some {$a}s";/effective; recommended methods of use
we want to express "she received some flowers", the flower in context should be in the plural (that is, it should be followed by s), but if you do not define the variable, the first echo will appear. Obviously we want the output to be $a rather than $as. So how do we usually handle this output?
Copy Code code as follows:
echo "She received some $a". S ";
echo "She received some". $a. " S "//These two habitual ways of writing should not be written in curly braces concise and clear?
Note: Whether {It appears in front of the $ or behind it, only the curly braces are considered as defining symbols if they are next to each other. Do not add spaces between them, or they will be treated as normal curly braces
echo "She received some {$a}s";//Output results are: She received some {flower}s
2. Complex syntactic rules (using curly braces to define expressions, etc., use with php4+):
Copy Code code as follows:
echo "Valid writing: {$arr [4][3]}";//valid; define multidimensional array
echo "Valid writing: {$arr [' foo '][3]}";//valid; When you use a multidimensional array in a string, be sure to enclose it in parentheses
echo "Effective writing: {$this->width}00";/effective; if not defined, it becomes $this->width00
echo "Valid writing: {$this->value[3]->name}";//valid; This example demonstrates the definition of a chained call
echo "valid: $name: {${$name}}";//valid; This example demonstrates an effect that is actually a variable variable
echo "valid notation: {${getname ()}}";/valid; This example demonstrates the function's return value as a variable name
echo "Valid release: {${$this->getname ()}}";/valid; This example demonstrates the return value of a function as a variable name
Note 1:echo "This write is valid: {GetName ()}"; The output is: ' Does this work:
{getName ()} '. Because the inside does not contain $, the curly braces are not treated as a qualifier
Note 2:echo "This write valid: {$arr [foo][3]}"; Before answering this question, let's start with an experiment:
Copy Code code as follows:
Error_reporting (E_all);
$arr = Array (' A ', ' B ', ' C ', ' d ' => ' e ');
echo "This is $arr [d]";//We find that writing is no problem, so what do we write like this?
echo $arr [d];
produced such a mistake:
notice:use of undefined constant d-assumed ' d '
Note: The undefined constant d should probably be used for ' d '
So if we modify the code as follows
Copy Code code as follows:
Error_reporting (E_all);
$arr = Array (' A ', ' B ', ' C ', ' d ' => ' e ');
Define (' F ', ' d ');
Echo $arr [F];
we found there was no problem this time. You can see that the index of an array in a string is no problem without a single quotation mark, but if it does not appear in the string, an error occurs, and the parsing of {$arr [foo][3]} in the string is parsed in a way that is not a string. So it's wrong to say that in a string, the array is delimited only with curly braces but not the index plus single quotes. This creates an error because the program parses the index without the single quotation mark as a constant. The correct wording should be:
echo "valid notation: {$arr [' foo '][3]}";
A special reminder: echo "This is $arr [d]", which can be parsed by the program, but only if the array is a one-dimensional array. The rigorous writing should be: echo "This is {$arr [' d ']}"; My student once argued with me at this point, he said: "Since the previous writing can produce results, why must use the latter one?" So, let's go ahead and modify the previous code
Copy Code code as follows:
Error_reporting (E_all);
$arr = Array (' A ', ' B ', ' C ', ' d ' =>array (' e ' => ' f '));
echo "This is $arr [d][e]";
can this be resolved correctly? I just want to tell you that curly braces are strictly necessary. Of course, if you're not my student then I don't care so much ...
Note 3:
Copy Code code as follows:
Error_reporting (E_all);
$arr = Array (' A ', ' B ', ' C ', ' d ');
echo "This is {$arr [2]} <br/>";
echo "This is {$arr [' 2 ']} <br/>";
executes the above code. The result is the same, why is this so? I can only tell you that PHP is a weak type language, and I'm not going to say anything about weak-type languages. Go Google yourself. Said so much, then the most embodiment of these rules and regulations are the advantages of the specific application where? ----SQL Statement
Copy Code code 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're going to play here.