PHP four ways to define strings

Source: Internet
Author: User

1. Introduction

In PHP, because it is a weakly typed language, the use of variables is not defined in advance.

Most of the time we use PHP for development, we use double quotes and single quotes to define strings. Since there are two ways, there must be a difference between them.

Also, in addition to the single-double-quote-definition string, PHP adds the two syntactic sugars, heredoc and nowdoc, to complement each other.

2, single quotation mark and double quotation mark 2.1, single quotation mark

In single quotes, any special characters are output as-is "except \ \, \ ' will be escaped output"

eg.

1 <? PHP 2 $var = ' This is a var! ' ; 3 Echo ' 666$var666, \ ', \ ', \ \ \ \ \ \ \ \ t, \$ ';

As we can see from the above example, the variable is not parsed, and the escape character with the backslash is only the single quote, the backslash is escaped output, the other is the direct output.

2.2. Double quotation marks

In double quotation marks, the variable will be parsed when the $ is encountered;

The escape character of the backslash is escaped in double quotation marks;

Escape character Codes Meaning of escape character
\ " Double quotes
\ ‘ Single quotation marks
\ \ Back slash
\ n Line break
\ r Carriage return character
\ t Tabs
\ $ Dollar sign

eg.

1 <? PHP 2 $var = ' This is a var! ' ; 3 Echo "666$var 666, \ ', \", \ \ \ \ \ \ \ \ t, \$ ";

As we can see from the above example, the variables in the double quotes are parsed, replaced with strings, and the other escape characters with backslashes are escaped accordingly.

2.3, the difference between single and double quotation marks the main difference between the two: 1,double quotation marks replace the value of a variable, and single quotes treat it as a string output. 2. Support for escaping
3, performance speed problem, because the string in double quotes needs to detect whether the variable containing the $ symbol decoration, so theoretically, the single quotation marks are relatively fast.

3, Heredoc and Nowdoc 3.1, Heredoc

Heredoc can be understood not to use double quotes to define strings, but the effect is consistent with using double quotes. "The double quotation marks are equivalent to ordinary characters at this time"

Syntax requirements:

1, opening and closing tags use the same string, usually written in uppercase letters.

2. No spaces or extra characters can appear after the start tag.

3. The closing tag must be written on the head, not indented and blank, and there should be a semicolon at the end of the tag.

4. Variables located between the opening and closing tags can be parsed normally, but the function is not allowed.

eg.

1<?PHP2 3 $var= ' This is Suifeng ';4 5 Echo<<< "EOT"6 7<! DOCTYPE html>89Ten<meta http-equiv= "Content-type" content= "text/html; Charset=utf8 "/> One<title> "With the Wind" </title> A -<body> -66666666666<br/> the"666666666" <br/> - $var<br/> -\$var<br/> -\n666<br> +</body> - +  AEOT;

From the printed result we can see that the double quotation marks are just one ordinary character, and the other effect is consistent with the string effect of the double quotation marks.

3.2, Nowdoc

Similarly, Nowdoc is defined without using single quotes, but the effect is equivalent to a string defined using single quotation marks. "The single quotation marks are equivalent to ordinary characters at this time"

Grammar with Heredoc

eg.

1<?PHP2 3 $var= ' This is Suifeng ';4 5 Echo<<< ' EOT '6 7<! DOCTYPE html>89Ten<meta http-equiv= "Content-type" content= "text/html; Charset=utf8 "/> One<title> "With the Wind" </title> A -<body> -' 02020 ' <br/> the\‘<br/> - 66666666666<br/> - "666666666" <br/> - $var <br/> + \ $var <br/> - \n666<br> + </body> A  at  -EOT;

Attention:

After the delimiter must not have any characters "especially the space character will sometimes not notice", otherwise there will be an error.

This error is reported when there is a space behind the EOT in the previous example: "Parse error: syntax error, unexpected ' << ' (T_SL)"

(These are some of their own ideas, if there is insufficient or wrong place please point out)

The leaf http://www.cnblogs.com/phpstudy2015-6/with the wind

Original address: http://www.cnblogs.com/phpstudy2015-6/p/8418822.html

Statement: This blog article for the original, only represents my work in the study in a certain time to summarize the views or conclusions. Please give the original link in the article page when reproduced

PHP four ways to define strings

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.