Let's talk about the difference between single and double quotation marks in PHP and the difference between php double quotation marks. Let's talk about the difference between single and double quotation marks in PHP. in php, you can use single quotation marks or double quotation marks. But the difference between single and double quotation marks in PHP will be explained again.
In PHP, you can use single quotation marks ''or double quotation marks.
However, the same single or double quotation marks must be used to define strings. for example, 'Hello World "and 'Hello world' are invalid strings.
What is the difference between single quotes and double quotes? Next, let's take a look at this article.
1. define a string
In PHP, you can use single quotes or double quotes to define strings. However, the same single or double quotation marks must be used to define strings. for example, 'hello' and 'hello' are invalid strings.
When defining a string, only one type of quotation mark is considered as a definition character, that is, single quotation marks or double quotation marks. Therefore, if a string starts with double quotation marks, only the double quotation marks are parsed by the analyzer. In this way, you can include any other character in the double quotation marks, or even single quotation marks. The following quotation marks are valid:
Php code
$s = "I am a 'single quote string' inside a double quote string"; $s = 'I am a "double quote string" inside a single quote string'; $s = "I am a 'single quote string' inside a double quote string"; $s = 'I am a "double quote string" inside a single quote string';
And the string "Why doesn' t" this "work? "Is divided into three sections. If you want to express double quotation marks in this string, you can use the escape character "\" (backslash) to convert it to "Why doesn't \" this \ "work? .
2. single and double quotation marks in string variables
PHP allows us to directly include string variables in double quotes. 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, while the content in the single quotation mark string is always considered as a common character. For example:
Php code
$ 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 $ foo \ n"; // print the result: foo is 2 (line feed) echo 'foo is $ foo \ n '; // print the result: foo is $ foo \ n $ 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 $ foo \ n"; // print the result: foo is 2 (line feed at the same time) echo 'foo is $ foo \ n'; // print the result: foo is $ foo \ n
As you can see, even the backslash in a single quotation mark loses its extended meaning (except for inserting the backslash \ and inserting the single quotation mark \'). Therefore, you should use double quotation marks when you want to replace variables in strings and include escape sequences such as \ n (linefeed. Single quotation mark strings can be used anywhere else. the processing speed of single quotation mark strings in the script is faster, because the PHP syntax analyzer treats single quotation mark strings in a simple way, the processing of double quotation marks is more complicated because the strings also need to be parsed, so the processing speed is slightly slower.
When you reference a complex variable combination in a string, some problems may occur. the following code works properly:
Php code
echo "value = $foo"; echo "value = $a[$i]"; 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]; // point (.) used for string connection (.)
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:
Php code
$ Var = 3; echo "value = {$ var}"; // print the result "value = 3" echo "value =\{ $ var }"; // print the result "value = {3}" $ var = 3; echo "value = {$ var }"; // print the result "value = 3" echo "value =\{ $ var}"; // print the result "value = {3 }"
3. in SQL statements
This is a common problem. the SQL statement for inserting a database uses single quotes to define strings. if you want to insert a string containing single quotes into the database, this SQL statement will fail.
For example:
$sql="insert into userinfo (username,password) Values('O'Kefee','123456')"
In this case, add the escape character backslash to the SQL statement,
That is :...... Values ('o \ 'keping ',......
Of course, you can also use the addslashes () function. the Function adds an escape character,
That is, $ s = addslashes ("O 'keyun ")...... Values ('". $ s ."',......
Another way is to set the magic-quotes option in php. ini. if this option is enabled, if the single quotation marks are included in the information submitted through the form, an escape character such as is automatically added. Therefore, you do not need to use other functions.
Supplement:This should start with double quotation marks and single quotation marks: the fields in double quotation marks will be interpreted by the compiler and then output as HTML code, but the fields in single quotation marks do not need to be interpreted and output directly.
For example:
$ Abc = 'I love u'; echo $ abc // The result is: I love u echo' $ ABC' // The result is: $ abc echo "$ abc" // The result is: I love u
Therefore, when assigning values to SQL statements in the database, you must use SQL = "select a, B, c from..." in double quotation marks. However, single quotation marks in SQL statements are used to quote field names.
For example:
select * from table where user='abc';
The SQL statement here can be directly written as SQL = "select * from table where user = 'abc '"
However, if it is as follows:
$ User = 'abc'; SQL1 = "select * from table where user = '". $ user. "'"; compare SQL2 = "select * from table where user = 'abc '"
I add spaces between single quotes and double quotes. I hope you can see it clearly.
That is, replacing 'ABC' with '". $ user."' is in a single quotation mark. It only splits the entire SQL string. SQL1 can be divided into the following three parts:
1: "select * from table where user = '"
2: $ user
3 :"'"
Use. to connect strings.
The above is a detailed explanation of the difference between single and double quotation marks in PHP. I hope it will be helpful to you. if you want to learn more, please stay tuned to the help House website!
In PHP, you can use single quotes ''or double quotes to define strings. But it must be...