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

Source: Internet
Author: User

In PHP, the definition of a string can be in English single quotes ', or you can use double quotes in English.

But you must use the same single or double quotes to define strings, such as ' Hello World ' and ' Hello World ' for illegal string definitions.

What's the difference between single and double quotes?

PHP allows us to include string variables directly in a double quote string.

The contents of a single quote string are always considered normal characters, so the content in single quotes is not escaped more efficiently.

Like what:

Copy Code code as follows:

$str = ' Hello ';
echo "STR is $STR"; Run Result: STR is hello
Echo ' str is $STR '; Run Result: STR is $STR

In PHP, variables in double quotes ($var) and special characters (such as \ r \ n) are escaped, and the content in single quotes is not escaped (so more efficient).
On the use of words,
I used to love to write this in a SQL string $sql = "SELECT * FROM table WHERE id = $id" so that the inside $id can be escaped, single quotes are not.

There is no difference between single and double quotes in JavaScript, as long as they are used in pairs.
I use single quotes in JavaScript most of the time because JavaScript and HTML deal with more, the output of HTML fragments do not need to escape the HTML attribute quotes.
In short, see the actual situation to use, how convenient how to use.

The above is this article about the single double quotes in PHP analysis of the difference, I hope you can enjoy

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.