Differences between single quotes and double quotes in php

Source: Internet
Author: User
There are many differences between single quotes and double quotes in php. As for the differences between single quotes and double quotes, I will use my understanding to explain the differences between single quotes and double quotes, variables in the middle of a single quotation mark are not executed. variables are treated as common strings, and php will parse the double quotation marks... there are many differences between single quotes and double quotes in php. As for the differences between single quotes and double quotes, I will use my understanding to explain the differences between single quotes and double quotes, variables in the middle of a single quotation mark are not executed. variables are treated as common strings, while php will parse variables in the middle if no variables exist, in this way, double quotation marks are charged for server resources in some ways.

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:

 "; Echo 'I am $ X'; // single quotes?>

Variables are enclosed in double quotation marks. the variables in single quotes are not (that is, the variable symbols in single quotes are not parsed). The variables in single quotes are not executed, and the double quotation marks are executed. the code is as follows:

$ Name = 'hello'; echo "the $ name";, the hello will be output. if it is a single quotation mark, the code is as follows:

$ Name = 'hello'; echo 'The $ name'; outputs the $ name. the main difference is that when a string references a complex variable combination, some problems may occur, and the following code will work 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, a new problem occurs. when we want to reference the curly braces in the 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 }"

Check the instance. the code is as follows:

 the value of $var2 is: ', $var2, "
"; //$var2 and $var1 have the same value "chris mao"$var2 = 'my name is $var2'; //modify $var2, the sametime $var1 was modifiedecho 'the value of $var1 is: ', $var1, "
"; //the value of $var1 is "my name is $var2"$var2 = "my new name is $var1"; //modify $var2, the sametime $var1 was modifiedecho 'the value of $var2 is: ', $var2, "
"; //the value of $var1 is "my new name is my name is $var2"echo 'the value of $var1 is: ', $var1, "
"; //the value of $var1 is "my new name is my name is $var2"?>


Tutorial link:

Reprint at will ~ However, please keep the tutorial address★

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.