A brief summary of the difference between single quotes and double quotes in a PHP string.

Source: Internet
Author: User

A brief summary of the difference between single quotes and double quotes in a PHP string.

Today, a friend asked me about the difference. Let's review it by the way.

Single quotes and double quotes are different:

  • "" Fields in double quotation marks are interpreted by the compiler and then output as HTML code.
  • ''Is output directly without being interpreted in single quotes.
  • The time for parsing single quotes is faster than that of double quotes.
  • Single quotation marks support \ escape characters, and double quotation marks support more escape characters.
$ Hello = 3; echo "hello is $ hello"; // print the result: hello is 3 echo 'Hello is $ hello'; // print the result: hello is $ hello echo "hello is $ hello \ n"; // print the result: hello is 2 (line feed) echo 'Hello is $ hello \ n '; // print the result: hello is $ hello \ n

PS:
Today, I saw foreigners mention the single quotes of PHP, and some interesting things are mentioned. The excerpt is as follows:
After installing the PHP extension Vulcan Logic discycler, you can see the intermediate code generated by PHP,
First:

echo "This is a string"; 

Will change:

ECHO    'This is a string' 

While

echo 'This is a string'; 

To

 ECHO    'This is a string' 

, Is the same
If yes

echo "This is a $variable"; 

The OPCODE generated by PHP is

INIT STRING ~0 2 ADD_STRING  ~0 ~0 'This' 3 ADD_STRING  ~0 ~0 ' ' 4 ADD_STRING  ~0 ~0 'is' 5 ADD_STRING  ~0 ~0 ' ' 6 ADD_STRING  ~0 ~0 'a' 7 ADD_STRING  ~0 ~0 ' ' 8 ADD_VAR    ~0 ~0 !0 9 ECHO      ~0 

While

echo "This is a " . $variable; 

Will become

CONCAT    ~0 'This is a ' !0 2 ECHO      ~0 

As you can see, the speed is much faster.

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.