What is the difference between single quotes and double quotes in PHP?

Source: Internet
Author: User
This article briefly introduces the differences between Single and Double quotation marks in php and compares the differences between Single and Double quotation marks in javascript. This makes it easier for us to better understand and practical. We recommend it to you.

This article briefly introduces the differences between Single and Double quotation marks in php and compares the differences between Single and Double quotation marks in javascript. This makes it easier for us to better understand and practical. We recommend it to you.

In PHP, you can use single quotation marks ''or double quotation marks" "to define strings "".

However, the same single or double quotation marks must be used to define strings. For example, 'Hello World "and" Hello World "are invalid strings.

What is the difference between single quotes and double quotes?

PHP allows us to directly include string variables in double quotation marks.

The content in a single quotation mark string is generally considered a common character, so the content in a single quotation mark is not escaped more efficiently.

For example:

The Code is as follows:


$ Str = 'hello ';
Echo "str is $ str"; // running result: str is hello
Echo 'str is $ str '; // running result: str is $ str

In php, variables ($ var) and special characters (\ r \ n) in double quotes are escaped, and the content in single quotes is not escaped (so it is more efficient ).
In use,
I used to like writing $ SQL = "SELECT * FROM table WHERE id = $ id" in the SQL string, so that the $ id in it can be escaped, and it won't work in single quotes.

There is no difference between single quotes and double quotes in JavaScript, as long as they are used in pairs.
Most of the single quotes I use in JavaScript are because Javascript has a lot of dealings with HTML. when outputting HTML fragments, you do not need to escape the attribute quotation marks in HTML.
In short, it depends on the actual situation and how to use it conveniently.

The above is an analysis of the difference between single double quotes in php. I hope you will like it.

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.