Home > Backend Development > PHP Tutorial > The effect of adding braces {} to output variables in PHP_PHP Tutorial

The effect of adding braces {} to output variables in PHP_PHP Tutorial

WBOY
Release: 2016-07-20 11:17:07
Original
938 people have browsed it

   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';
echo $str{0}; // The output is h
echo $str{1}; // The output is e

代码如下  

$str = 'hello';
echo $str{0}; // 输出为 h
echo $str{1}; // 输出为 e

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 ).

代码如下  

if ( !isset ( $str{5} ) ) 就比 if ( strlen ( $str ) < 5 ) 好。

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/372084.htmlTechArticlephp output variables are enclosed in curly brackets. What is the writing method? Look at the following code: Copy the code as follows? php header (Content-Type: text/html; charset=utf-8); $test=1 variable 1; //echo is preceded by...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template