- $ A = 'flower ';
- Echo "She has ed some $ ";
- // Invalid; the letter s is regarded as a valid variable name component element, but the variable here is $
- Echo "She has ed some $ {a} s"; // valid
- Echo "She has ed some {$ a} s"; // valid; recommended usage
What we want to express is that "she receives some flowers", and the flower in the context should adopt the plural form (that is to say, S should be added later), but if we do not define the variable, the first echo is displayed. Obviously, we want to output $ a instead of $. So how do we deal with this output?
- Echo "She has ed some $ a". "s ";
- Echo "She has ed some". $ a. "s ";
- // Should the two habitual writing methods be concise and clear without curly braces?
Note: No matter whether it is in front or back of $, curly braces are considered as defining symbols only when the two are close to each other. Do not add spaces between them. Otherwise, they will be processed as normal curly braces.
- Echo "She has ed some {$ a} s ";
- // The output result is: She has ed some {flower} s
2,Complex syntax rules(Use curly brackets to define expressions, etc., with PHP4 + ):
- Echo "valid syntax: {$ arr [4] [3]}";
- // Valid; defines multi-dimensional arrays
- Echo "valid syntax: {$ arr ['foo'] [3]}";
- // Valid; when using a multi-dimensional array in a string, enclose it with parentheses.
- Echo "valid syntax: {$ this-> width} 00 ";
- // Valid; if not defined, it will become $ this-> width00
- Echo "valid syntax: {$ this-> value [3]-> name }";
- // Valid; this example demonstrates defining chained calls
- Echo "valid syntax: $ name :{$ {$ name }}";
- // Valid; the effect demonstrated in this example is actually a variable
- Echo "valid syntax: {$ {getName ()}}";
- // Valid. In this example, the return value of the function is used as the variable name.
- Echo "valid delivery: {$ this-> getName ()}}";
- // Valid. In this example, the return value of the function is used as the variable name.
- Note 1: echo "is this write valid: {getName ()}"; the output result is: 'Is this write valid:
- {GetName ()}'. Because it does not contain $, curly brackets are not considered as delimiters.
- Note 2: echo "is this valid? {$ arr [foo] [3]}"; before answering this question, let's start an experiment:
- Error_reporting (E_ALL );
- $ Arr = array ('A', 'B', 'C', 'D' => 'e ');
- Echo "This is $ arr [d]";
- // We find that writing like this is okay. What about writing like below?
- Echo $ arr [d];
- This error occurs:
- Notice: Use of undefined constant d-assumed 'D'
- Note: an undefined constant d may be used as 'D'
- If we modify the code as follows:
- Error_reporting (E_ALL );
- $ Arr = array ('A', 'B', 'C', 'D' => 'e ');
- Define ('F', 'D ');
- Echo $ arr [f];
No problem this time. It can be seen that there is no problem in adding single quotation marks to the index of the array in the string, but if this writing method does not appear in the string, an error will be reported, in the string, {$ arr [foo] [3]} is parsed in a non-string manner. Therefore, it is wrong to add only curly brackets to the array in the string but not to add single quotation marks to the index. Because the program will parse the index without single quotes as a constant, this produces an error. The correct syntax should be echo "valid syntax: {$ arr ['foo'] [3]}"; note: echo "This is $ arr [d]"; This method can be parsed by a program, but it is limited to the case where the array is a one-dimensional array. The rigorous statement should be echo "This is {$ arr ['D']}". my students once argued with me at This point. He said: since the previous method can produce results, why must it be followed? Then, 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 curly braces are strictly necessary.
Note 3:
- Error_reporting (E_ALL );
- $ Arr = array ('A', 'B', 'C', 'D ');
- Echo "This is {$ arr [2]}
- ";
- Echo "This is {$ arr ['2']}
- ";
The result of executing the above code is the same. why? Because PHP is a weak language.
- ---- SQL statement
- // Example 1:
- $ SQL1 = "select * from table where id = {$ _ GET ['id']}";
- // Example 2:
- $ SQL2 = "select * from table where id = {$ this-> id }";
|