Difference between double quotes and single quotes in PHP (for beginners)

Source: Internet
Author: User

Punctuation Marks in programming languages are not something we can add to others and know what you want to say, but computers are different. improper use may cause errors and cause a lot of trouble to you, the use of single quotes and double quotes in PHP is a common problem for new users. The difference between the two is described in this article. I hope it will help new users!

In PHP, a string is usually defined in a pair of quotation marks, such:

'I am a string in single quotes'
"I am a string in double quotes"

The PHP syntax analyzer uses pair quotation marks to determine a string. Therefore, all strings must use the same single or double
To define start and end. For example, the following string definition is illegal:

"I am not a valid string since I have unmatching quote marks'
'Me neither! "

When defining a string, only one type of quotation mark is considered as a definition character, that is, single quotation marks or double quotation marks. Therefore, if a string is
The quotation marks are parsed by the analyzer. In this way, you can include any other character in the double quotation mark string, or even a single cited
. The following quotation marks are valid: www.2cto.com

$ S = "I am a' single q the above example attempts to include double quotation marks in double quotation marks, and the analyzer considers the string to be closed when the second double quotation marks are encountered

. To enclose quotation marks, the analyzer must ignore the original meaning of the regular quotation marks in a string.
Add a backslash to tell PHP: this quotation mark is a part of the string. The correct expression is as follows:

"Why doesn't \" that \ "work? "

A common problem in English strings is the use of the marker, because it is a single quotation mark, which is very common in English strings.
(In english ). You must handle these characters with caution:

You \ 'd better escape your a post rophes'

We can see that the backslash has its special meaning in the string. When we need to include the backslash itself in the string, we need
Add a backslash before the symbol. For example:

$ File = "c: \ windows \ system. ini ";
Echo $ file; // The printed result is: c: windows system. ini.
$ File = "c :\\ windows \ system. ini ";
Echo $ file; // The printed result is: c: \ windows \ system. ini.
Uote string 'inside a double quote string ";
$ S = 'I am a "double quote string" inside a single quote string ';


Author: Feiyu 1993

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.