Differences between single quotes and double quotes in php

Source: Internet
Author: User

Single quotation marks and double quotation marks are roughly the same, but it is very important that the variable is referenced. You can understand the following code by running the following code:

The code is as follows: Copy code

<? Php

$ X = "China ";

Echo "I am $ x"; // double quotation marks

Echo "<br/> ";

Echo 'I am $ X'; // single quotes

?>


Variables are enclosed in double quotes, but not in single quotes (that is, the variable symbols in single quotes are not parsed)


Variables in single quotes are not executed.

Double quotation marks are executed.

For example

The code is as follows: Copy code

$ Name = 'hello ';

Echo "the $ name ";

Will output the hello

If it is a single quotation mark

The code is as follows: Copy code

$ Name = 'hello ';

Echo 'The $ name ';

The $ name

This is the main difference.


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

The code is as follows: Copy code
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:

The code is as follows: Copy code
Echo 'value = '. $ a [$ I] [$ j];

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

The code is as follows: Copy code
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:

The code is as follows: Copy code
$ Var = 3;
Echo "value = {$ var}"; // print the result "value = 3"
Echo "value = {$ var}"; // print the result "value = {3 }"


View instances

The code is as follows: Copy code

$ Var1 = "chris mao"; // assign the value "chirs mao" to $ var1
$ Var2 = & $ var1; // reference $ var1 via $ var2
Echo '<B> the value of $ var2 is: </B>', $ var2, "<br> "; // $ var2 and $ var1 have the same value "chris mao"
$ Var2 = 'My name is $ var2'; // modify $ var2, the sametime $ var1 was modified
Echo '<B> the value of $ var1 is: </B>', $ var1, "<br> "; // the value of $ var1 is "my name is $ var2"
$ Var2 = "my new name is $ var1"; // modify $ var2, the sametime $ var1 was modified
Echo '<B> the value of $ var2 is: </B>', $ var2, "<br> "; // the value of $ var1 is "my new name is my name is $ var2"
Echo '<B> the value of $ var1 is: </B>', $ var1, "<br> "; // the value of $ var1 is "my new name is my name is $ var2"

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.