In php, braces {} are commonly used for class array or condition judgment, but in variables, what is the use of this syntax {$ test, next I will introduce you to the php output variable with parentheses.
In php, braces {} are commonly used for class array or condition judgment. but in the variable, what is the use of this syntax {$ test}? I will introduce it to you.
Php output variables increase brackets. what is the writing method? See the following code:
The instance code is as follows:
-
- Header ("Content-Type: text/html; charset = utf-8 ");
- $ Test = "1 variable 1 ";
- // Echo "there is a string aa". $ test. "bb string ";
- Echo "there are strings following aa {$ test} bb ";
- ?>
We can see that the php output variable adding brackets is the same as using the. operator to output the variable string, and summarize the following three points to help understand the php output variable adding brackets:
1. the variable in {} is processed according to the variable during execution;
2. use special methods to reference variables in strings, so that you do not use the. operator to reduce the input of code;
3. prevent variable names and strings from being connected together.
Appendix:
Function of braces {} in string variables:
The PHP variable is followed by a braces {}, which are filled with numbers, that is, the character corresponding to the PHP variable serial number.
The instance code is as follows:
- $ Str = 'hello ';
- Echo $ str {0}; // The output is h.
- Echo $ str {1}; // The output is e.
If you want to check whether the length of a string is sufficient, consider replacing the strlen function with this braces {} and isset, because isset is a language structure and strlen is a function, therefore, using isset is more efficient than using strlen.
For example, to determine whether the length of a string is less than 5:
The instance code is as follows:
- If (! Isset ($ str {5}) is better than if (strlen ($ str) <5.