Four ways to define strings in PHP

Source: Internet
Author: User
The following article will show you how to use PHP to define the string, the definition of the string is divided into four ways of definition, the need for friends can refer to, together and small to understand how to define the string bar.

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 and double quotation marks

2.1. Single quotation mark

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

eg.

<?php$var = ' This is a var! '; 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;

<?php$var = ' This is a var! '; echo "666$var 666, \", \ "\ \ \ \ \ \ \ \ t, \$";
Escape character Codes Meaning of escape character
\" double quotation mark
\ ' single quotation mark
\ \ backslash
\ n line break
\ R return character
\ t tab
\ $ Dollar sign

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 above two main differences:

1. Double quotes 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.

<?php $var = ' this is Suifeng ';  echo <<< "EOT" <! DOCTYPE html> 


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.

<?php  $var = ' this is Suifeng ';  echo <<< ' EOT '  <! DOCTYPE html> 


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 will be reported in the following space after the EOT in the previous example: "Parse error:syntax error, unexpected ' << ' (T_SL)"

Summarize

Described above is a small part of the PHP definition string to introduce four ways to explain, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Thank you very much for the support of the Scripting House website!

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.