1. Simple syntax rules (define variable names with curly braces, applicable to all versions of PHP):
$a ='flower';
echo "She received some $as";// Invalid; the letter s will be regarded as a valid variable name to form an element, but the variable here is $a
echo "She received some ${a}s";// Valid
echo "She received some {$a}s";// Valid; recommended usage
What we want to express is that "she received some flowers". The flower in the context should be in the plural form (that is, S should be added after it), but if there is no definition of the variable, the first echo will occur. Obviously what we want to output is$aInstead$as。 So how do we usually handle this output?
echo "She received some $a"."s";
echo "She received some ".$a."s";// These two habitual writing methods should not be simple and clear in the use of curly braces, right?
Note: Whether { appears before or after $, curly braces will be regarded as defining symbols only when the two are next to each other. Don't add spaces between them, otherwise they will be treated as ordinary curly braces.
echo "She received some { $a}s";// The output result is: She received some { flower}s
2. Complex syntactic rules (defining expressions with curly braces, etc., using PHP4+):
echo "Effective writing: {$arr[4][3]}";// Valid; define multi-dimensional arrays
echo "Effective writing: {$arr['foo'][3]}";// Valid; when using multi-dimensional arrays in strings, be sure to enclose them in brackets
echo "Effective writing: {$this->width}00";// Valid; if not defined, it will become $this->width00
echo "Effective writing: {$this->value[3]->name}";// Valid; this example demonstrates defining chain calls
echo "Effective writing: $name: {${$name}}";// Valid; the effect demonstrated by this example is actually a mutable variable
echo "Valid writing method: {${getName()}}";// Valid; this example demonstrates the return value of a function as a variable name
echo "Valid issuance: {${$this->getName()}}";// Valid; this example demonstrates the return value of the function as the variable name
Note 1: echo "Is this effective to write: {getName()}"; the output result is: 'Is this effective to write:
{getName()}'. Because it does not contain $, curly braces will not be used as delimiters
Note 2: echo "Is this valid to write: {$arr[foo][3]}"; Before answering this question, let's conduct an experiment:
error_reporting(E_ALL);
$arr = array('a','b','c','d'=>'e');
echo "This is $arr[d]";// We found that there is no problem writing like this, so what about we writing like below?
echo $arr[d];
An error like this occurred:
Notice: Use of undefined constant d - assumed 'd'
Note: Undefined constant d is used, probably should be 'd'
Then if we modify the code like below
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. It can be seen that there is no problem with the index of the array in a string without single quotes, but if this writing method does not appear in a string, an error will be reported, and the parsing of {$arr[foo][3]} in the string is parsed in a non-string manner. Therefore, it is wrong to write that only curly braces are added to the array in a string and not single quotes are added to the index. Because the program will parse indexes without single quotes as constants, this creates an error. The correct way to write it should be:
echo "Effective writing: {$arr['foo'][3]}";
A special reminder: although this writing method can be parsed by the program, this is limited to the case where the array is a one-dimensional array. The rigorous writing method should be: echo "This is {$arr['d']}"; My student once argued with me on this point, and he said: Since the previous writing method can produce results, why do you have to use the latter writing method? 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 be correctly parsed? I just want to tell you that adding curly braces is necessary. Of course, if you are not my student, then I can't control that much...
Note 3:
error_reporting(E_ALL);
$arr = array('a','b','c','d');
echo "This is {$arr[2]} <br />";
echo "This is {$arr['2']} <br />";
Execute the above code. The result is the same, why is this happening? I can only tell you that PHP is a weak-type language. As for what a weak-type language is, I won't say more here. Go to Google yourself. Having said so much, where is the specific application that best reflects the advantages of these syntactic rules? ----SQL statement
// Example 1:
$SQL1 ="select * from table where id={$_GET['id']}";// Example 2:
$SQL2 ="select * from table where id={$this->id}";
OK, we'll end up playing with curly braces.
Copy the codeThe code is as follows:
$a ='flower';
echo "She received some $as";// Invalid; the letter s will be regarded as a valid variable name to form an element, but the variable here is $a
echo "She received some ${a}s";// Valid
echo "She received some {$a}s";// Valid; recommended usage
What we want to express is that "she received some flowers". The flower in the context should be in the plural form (that is, S should be added after it), but if there is no definition of the variable, the first echo will occur. Obviously what we want to output is$aInstead$as。 So how do we usually handle this output?
Copy the codeThe code is as follows:
echo "She received some $a"."s";
echo "She received some ".$a."s";// These two habitual writing methods should not be simple and clear in the use of curly braces, right?
Note: Whether { appears before or after $, curly braces will be regarded as defining symbols only when the two are next to each other. Don't add spaces between them, otherwise they will be treated as ordinary curly braces.
echo "She received some { $a}s";// The output result is: She received some { flower}s
2. Complex syntactic rules (defining expressions with curly braces, etc., using PHP4+):
Copy the codeThe code is as follows:
echo "Effective writing: {$arr[4][3]}";// Valid; define multi-dimensional arrays
echo "Effective writing: {$arr['foo'][3]}";// Valid; when using multi-dimensional arrays in strings, be sure to enclose them in brackets
echo "Effective writing: {$this->width}00";// Valid; if not defined, it will become $this->width00
echo "Effective writing: {$this->value[3]->name}";// Valid; this example demonstrates defining chain calls
echo "Effective writing: $name: {${$name}}";// Valid; the effect demonstrated by this example is actually a mutable variable
echo "Valid writing method: {${getName()}}";// Valid; this example demonstrates the return value of a function as a variable name
echo "Valid issuance: {${$this->getName()}}";// Valid; this example demonstrates the return value of the function as the variable name
Note 1: echo "Is this effective to write: {getName()}"; the output result is: 'Is this effective to write:
{getName()}'. Because it does not contain $, curly braces will not be used as delimiters
Note 2: echo "Is this valid to write: {$arr[foo][3]}"; Before answering this question, let's conduct an experiment:
Copy the codeThe code is as follows:
error_reporting(E_ALL);
$arr = array('a','b','c','d'=>'e');
echo "This is $arr[d]";// We found that there is no problem writing like this, so what about we writing like below?
echo $arr[d];
An error like this occurred:
Notice: Use of undefined constant d - assumed 'd'
Note: Undefined constant d is used, probably should be 'd'
Then if we modify the code like below
Copy the codeThe code is 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. It can be seen that there is no problem with the index of the array in a string without single quotes, but if this writing method does not appear in a string, an error will be reported, and the parsing of {$arr[foo][3]} in the string is parsed in a non-string manner. Therefore, it is wrong to write that only curly braces are added to the array in a string and not single quotes are added to the index. Because the program will parse indexes without single quotes as constants, this creates an error. The correct way to write it should be:
echo "Effective writing: {$arr['foo'][3]}";
A special reminder: although this writing method can be parsed by the program, this is limited to the case where the array is a one-dimensional array. The rigorous writing method should be: echo "This is {$arr['d']}"; My student once argued with me on this point, and he said: Since the previous writing method can produce results, why do you have to use the latter writing method? So, let's continue to modify the previous code
Copy the 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 be correctly parsed? I just want to tell you that adding curly braces is necessary. Of course, if you are not my student, then I can't control that much...
Note 3:
Copy the codeThe code is as follows:
error_reporting(E_ALL);
$arr = array('a','b','c','d');
echo "This is {$arr[2]} <br />";
echo "This is {$arr['2']} <br />";
Execute the above code. The result is the same, why is this happening? I can only tell you that PHP is a weak-type language. As for what a weak-type language is, I won't say more here. Go to Google yourself. Having said so much, where is the specific application that best reflects the advantages of these syntactic rules? ----SQL statement
Copy the codeThe code is as follows:
// Example 1:
$SQL1 ="select * from table where id={$_GET['id']}";// Example 2:
$SQL2 ="select * from table where id={$this->id}";
OK, we'll end up playing with curly braces.