1. Simple syntax rules (use curly braces to define variable names for all versions of PHP):
$a = ' flower ';
echo "She received some $as";
Invalid; the letter S will be treated as a valid variable name, but the variable here is $a
echo "She received some ${a}s"; Effective
echo "She received some {$a}s"; The recommended method of use
What we want to say is "she received some flowers" and 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?
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 when the two 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";
The result of the output is: She received some {Flower}s
2. Complex syntactic rules (using curly braces to define expressions, etc., use with php4+):
echo "valid notation: {$arr [4][3]}";
To have a valid or defined 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 "Effective writing: {$this->width}00";
Effective, if not defined, it becomes $this->width00
echo "valid notation: {$this->value[3]->name}";
Valid; This example demonstrates the definition of a chained call
echo "Valid notation: $name: {${$name}}";
Effective; This example demonstrates an effect that is actually a variable variable
echo "valid notation: {${getname ()}}";
Valid; This example shows the return value of a function as a variable name
echo "effectively issued: {${$this->getname ()}}";
Valid; This example shows 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 it contains no $, the curly braces are not treated as a qualifier.
Note 2:echo "This write is valid: {$arr [foo][3]}"; Before answering 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 that writing is no problem, so what do we write as follows?
echo $arr [d];
This error has occurred:
Notice:use of undefined constant d-assumed ' d '
Note: The undefined constant d should probably be used as ' d '
So if we modify the code as follows
Error_reporting (E_all);
$arr = Array (' A ', ' B ', ' C ', ' d ' => ' e ');
Define (' F ', ' d ');
Echo $arr [F];
We found that 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.
Error_reporting (E_all);
$arr = Array (' A ', ' B ', ' C ',
' d ' =>array (' e ' => ' F ')
);
echo "This is $arr [d][e]";
Can it be resolved correctly? I just want to tell you that curly braces are strictly necessary. Of course, if you are not my student then I can't control so much ...
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 this? I can only tell you that PHP is a weakly typed language, and I'm not going to say anything about weak-type language here. Go Google yourself. Said so much, then the best embodiment of these rules of the specific advantages of the application where is it?----SQL statement
Example one:
$SQL 1 = "SELECT * from table where id={$_get[' id ']}";
Example two:
$SQL 2 = "SELECT * from table where id={$this->id}";