Difference between single quotes and double quotes in php

Source: Internet
Author: User
Tags php tutorial

Difference between single quotes and double quotes in the php Tutorial

Any variable ($ var) or special escape characters (such as "t r n") in quotation marks will not be parsed, so php will be faster to parse, only escape characters such as "'" and "" can escape single quotes and backslash themselves;

Use variables in strings

This feature allows you to stick a large number of simple strings without using the concatenation symbol. Php allows us to directly include words in double quotation marks
String variables, we can find that the processing results of the following two strings are the same.

$ Full_name = $ first_name. ''. $ last_name;
$ Full_name = "$ first_name $ last_name ";

The processing of single and double quotation marks is different in php. The content in the double quotation mark string can be interpreted and replaced.
The content in the string is generally considered a common character. For example:

When you reference a complex variable combination in a string, some problems may occur. The following code works properly:

Echo "value = $ foo ";
Echo "value = $ a [$ I]";

However, the following code cannot get the expected results:

Echo "value = $ a [$ I] [$ j]"; // we want to print an element of the two-dimensional array $.

To avoid potential problems in the use of these strings, we usually separate complex variables from the strings, like this:

Echo 'value = '. $ a [$ I] [$ j];

Another way is to enclose complex variables in curly brackets so that the syntax analyzer can correctly identify them:

Echo "value = {$ a [$ I] [$ j]}" // print an element of a two-dimensional array $

In this way, new problems have emerged. When we want to reference the curly braces in a string, remember to use the escape character:

$ Var = 3;
Echo "value = {$ var}"; // print the result "value = 3"
Echo "value = {$ var}"; // print the result "value = {3 }"


$ Foo = 2;
Echo "foo is $ foo"; // print the result: foo is 2
Echo 'foo is $ Foo'; // print the result: foo is $ foo
Echo "foo is $ foon"; // print the result: foo is 2 (line feed at the same time)
Echo 'foo is $ foon '; // print the result: foo is $ foon

 

In double quotation marks, the variable ($ var) value is substituted into the string, and special escape characters are also parsed into a specific single character. There are also some special functional escaping for the above two features, for example, "$" and "{$ array ['key']}. In this way, although programming is more convenient, php parsing is also slow;


Variables inside the quotation marks are not executed.
Double quotation marks are executed.

For example
$ Name = 'hello ';
Echo "the $ name ";

Will output the hello

If it is a single quotation mark

$ Name = 'hello ';
Echo 'The $ name ';

The $ name

The main difference is that it doesn't matter if you like it.
We recommend that you maintain consistency. For example, you can use single quotes to maintain consistency in other places, and the double quotation marks also apply.

 

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.