Home > Article > Backend Development > The effect of adding braces {} to output variables in PHP_PHP Tutorial
php输出变量加大括号,这是什么写法?看下面一段代码:
代码如下 | 复制代码 |
| |
代码如下 | 复制代码 |
header("Content-Type:text/html; charset=utf-8"); $test="1变量1"; //echo "前面有字符串aa ".$test." bb后面字符串"; echo "前面有字符串aa {$test} bb后面字符串"; ?> |
header("Content-Type:text/html; charset=utf-8");
$test="1变量1";
//echo "前面有字符串aa ".$test." bb后面字符串";
echo "前面有字符串aa {$test} bb后面字符串";
?>
It can be seen that the effect of adding braces to PHP output variables is the same as using the . operator to output variable strings. The following three points are summarized to help understand the role of adding braces {} to PHP output variables:
1. Indicates that the value inside {} is a variable and will be processed according to the variable during execution;
2. The special inclusion method used to reference variables in a string, so that the . operator can be used without using it, thereby reducing the amount of code input;
3. Prevent the variable name from being concatenated with the following string.
Attachment:
The role of braces {} in string variables:
Add a brace {} after the PHP variable and fill in the numbers, which refers to the characters corresponding to the corresponding serial numbers of the PHP variable.
For example:
The code is as follows | |||||
$str = 'hello';
|
If you want to check whether a string meets a certain length, you can consider using braces {} plus isset to replace the strlen function. Because isset is a language structure and strlen is a function, using isset is more efficient than using strlen. high.
For example, to determine whether the length of a string is less than 5:
The code is as follows | |||||
if ( !isset ( $str{5} ) ) is better than if ( strlen ( $str ) < 5 ).
|