Php variable adding brackets {} role _ PHP Tutorial

Source: Internet
Author: User
The output variable in php adds brackets. Php output variables increase brackets. what is the writing method? See the following code: Copy the code as follows? Phpheader (Content-Type: texthtml; charsetutf-8); $ test1 variable 1; echo front php output variable increase brackets, what is this writing? See the following code:

The 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:

Add a braces {} next to the PHP variable, and fill in a number, which is the character corresponding to the PHP variable serial number.

For example:

The 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 code is as follows:

If (! Isset ($ str {5}) is better than if (strlen ($ str) <5.

Why? See the following code: the code is as follows? Php header (Content-Type: text/html; charset = utf-8); $ test = 1 variable 1; // echo is preceded...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.