-
- $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
-
Copy code
What we want to express is "She received some ${a}s"; "To some flowers", the word "flower" in the context should be in plural form (that is, S should be added after it), but if the variable is not defined in any way, the first echo situation will occur. Obviously we want the output to be $a rather than $as. So how do we usually process this output?
-
- echo "She received some $a"."s";
- echo "She received some ".$a."s";
- // These two customary writing methods should not include curly brackets Is the writing concise and clear?
-
Copy code
Note: No matter whether { appears before or after $, curly braces will be regarded as delimiting symbols only when they are immediately adjacent. Do not 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
Copy code
2, Complex syntax rules (use Curly braces delimit expressions, etc., used with PHP4+):
-
- echo "Valid writing: {$arr[4][3]}";
- // Valid; define multi-dimensional arrays
- echo "Valid writing: {$arr['foo'][3] }";
- // Valid; when using a multi-dimensional array in a string, be sure to enclose it in parentheses
- echo "Valid writing: {$this->width}00";
- // Valid; if If not defined, it will become $this->width00
- echo "Effective writing: {$this->value[3]->name}";
- // Valid; this example demonstrates the definition chain Call
- echo "Valid writing method: $name: {${$name}}";
- // Valid; the effect demonstrated in this example is actually a variable variable
- echo "Valid writing method: {${getName() }}";
- // Valid; this example demonstrates using the return value of the function as a variable name
- echo "Valid delivery: {${$this->getName()}}";
- // Valid; the The example demonstrates using the return value of the function as a variable name. Note 1: echo "Is it valid to write like this: {getName()}"; the output result is: 'Is it valid to write like this:
- {getName()}'. Because there is no $ in it, the curly braces will not be used as delimiters
- Note 2: echo "Is it valid to write like this: {$arr[foo][3]}"; Before answering this question, let's conduct an experiment first :
- error_reporting(E_ALL);
- $arr = array('a', 'b', 'c', 'd'=>'e');
- echo "This is $arr[d]";
- / / We found that there is no problem in writing like this, so what if we write like this?
- echo $arr[d];
- produces this error:
- Notice: Use of undefined constant d - assumed 'd'
- Note: Use of undefined constant d - assumed 'd'
- So if we like Modify the code as follows
- error_reporting(E_ALL);
- $arr = array('a', 'b', 'c', 'd'=>'e');
- define('f', 'd ');
- echo $arr[f];
-
-
Copy code
No problem this time. It can be seen that there is no problem if the index of the array in the string is not enclosed in single quotes, but if this writing method does not appear in the string, an error will be reported, and for {$arr[foo][3]} in the string Parsing is based on non-string parsing. Therefore, it is wrong to only add curly braces to delimit the array in the string without adding single quotes to the index. Because the program will parse the index without single quotes as a constant, this generates an error. The correct way to write it should be:
echo "Valid writing: {$arr['foo'][3]}";
A special reminder: echo "This is $arr[d]"; although this writing method can be parsed by the program, it is limited to the case where the array is a one-dimensional array. The strict way of writing should be: echo "This is {$arr['d']}"; My student once argued with me on this point. He said: Since the previous way of writing can produce results, why must we use it? What about the latter way of writing? 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]";
-
-
Copy code Can this still be parsed correctly? I just want to tell you that adding curly braces is strictly necessary.
Note 3:
-
- error_reporting(E_ALL);
- $arr = array('a', 'b', 'c', 'd');
- echo "This is {$arr[2]}
- ";
- echo "This is {$arr['2']}
- ";
-
Copy the code
Execute the above code, the result is the same, why is this? Because PHP is a weakly typed language.
-
- ----SQL statement
- //Example 1:
- $SQL1 = "select * from table where id={$_GET['id']}";
- //Example 2:
- $SQL2 = "select * from table where id={$this->id}";
Copy code
|