Relationship and difference between single and double quotation marks in PHP

Source: Internet
Author: User
: This article mainly introduces the relationship and difference between single and double quotation marks in PHP. For more information about PHP tutorials, see. Today, we will explain the differences through several practical small cases. let's take a closer look!

// Double quotation marks

// This syntax is incorrect because it regards hello as a double quotation mark character, while world will become an unknown character.

$ Str1 = "hello" world "".'
';

// Correct syntax: add the escape character \ to the world with double quotation marks so that no boundary ambiguity will occur.
$ Str1 = "hello \" world \"".'
';
Echo $ str1;
$ Str2 = "hello \ n \ r \ t world ".'
'; // \ N \ r: line feed carriage return. double quotation marks can be parsed.
Echo $ str2;
$ Str3 = "hello $ str1 ".'
'; // At this time, the system parses $ str1 into a variable and calls $ str1
Echo $ str3;
$ Str4 = "hello \ $ str1"; // after $ matches the escape character, the system regards $ str1 as a normal string instead of a variable.
Echo $ str4;
// Single quotes
$ Str1 = 'Hello \ n \ r \ t world '.'
'; // Single quotes cannot be parsed and identified \ n \ r \ t, which will be directly output as common characters
Echo $ str1;
$ Str2 = 'Hello $ str1'; // single quotes cannot parse and recognize the $ symbol. $ str1 is output as a common character directly.
Echo $ str2;
The double quotation marks are mixed and explained. the following three conclusions are obtained:

1. single quotes cannot be escaped too much. they can only be escaped \ ', while double quotes can be escaped \ "\ n \ r \ t
2. $ variable characters cannot be parsed in single quotes, while double quotation marks can
3. because single quotes do not need to be considered in many cases, single quotes are faster than double quotes!

I believe that through tonight's analysis, you will not have any questions about the single double quotation marks in PHP in the future! If you think it is good, you can

Thank you!


The above describes the relationship and difference between single and double quotation marks in PHP, including the content, and hope to help those who are interested in the PHP Tutorial.

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.