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" |