Introduction to the difference and usage of single quotes and double quotes in php_PHP tutorial
WBOY
Release: 2016-07-13 10:55:32
Original
1545 people have browsed it
The article uses a large number of implementations to introduce the usage and difference between single quotes and double quotes. Students who need to know more can refer to this article carefully.
In PHP, usually a string is defined within a pair of quotes
$out = str_replace(array('rn', 'r', 'n'), '', $out);PHP provides three ways to define strings: single quotes, double quotes, local documents (English called a here document or heredoc).
Single quotes:
Using single quotes is the most efficient method because PHP does not check built-in variables and escape sequences in single-quoted strings. The only characters that need to be escaped are backslashes and single quotes themselves.
Double quotes:
Built-in variables and escape sequences are checked, but escaped single quotes are not recognized. This also shows what is wrong with the code at the beginning. The correct approach is to use double quotes to define the escape sequence for newlines:
Check all built-in variables and escape sequences, double quotes do not need to be escaped. For example:
代码如下
复制代码
echo <<
this is a "here document" example.
just for test.
The code is as follows
Copy code
echo <<
this is a "here document" example.
just for test.
EOT; Simply record it to deepen your impression.
代码如下
复制代码
'I am a string in single quotes'
"I am a string in double quotes"
, such as:
The code is as follows
Copy code
'I am a string in single quotes'
"I am a string in double quotes"
代码如下
复制代码
"I am not a valid string since I have unmatching quote marks'
'Me neither!"
The PHP parser uses pairs of quotation marks to determine a string. Therefore, all strings must use the same single or double <🎜>
Quotation marks to define start and end. For example, the following string definition is illegal: <🎜>
<🎜> <🎜>
The code is as follows
Copy code
<🎜> <🎜>
<🎜>"I am not a valid string since I have unmatching quote marks' <🎜>
'Me neither!"<🎜>
When defining a string, only one kind of quotation mark is considered as the delimiter, that is, single quotation mark or double quotation mark. So, if a string is represented by a double quote
, then only double quotes are parsed by the parser. This way, you can include any other character within the double-quoted string, even single-quoted
Number. The following quotation mark strings are legal:
The code is as follows
Copy 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';
The above example attempts to include double quotes within a double quote string, and the parser considers the string to be terminated when it encounters the second double quote
Finished. To achieve the purpose of including quotation marks, the parser must ignore its original meaning when encountering ordinary quotation marks in the string. We use the quotation mark
Add a backslash in front of it to tell PHP that this quote is part of the string. The correct representation is like this:
The code is as follows
Copy code
"Why doesn't "that" work?"
A common problem in English strings is the use of apostrophe ', because it is a single quote, and it is very common in English strings
(English possessive case). You have to be careful with these characters:
<🎜> You can see that backslash has its special meaning in strings. When we need to include the backslash itself in the string, we need to include it in <🎜>
This symbol is preceded by an extra backslash. For example: <🎜>
The code is as follows
Copy code
<🎜>$file = "c:windowssystem.ini"; <🎜>
echo $file; // The print result is: c:windowssystem.ini <🎜>
$file = "c:windowssystem.ini"; <🎜>
echo $file; // The print result is: c:windowssystem.ini<🎜>
<🎜> Another way to define strings that eliminates the trouble of special characters and makes it easier to quote longer texts. The string definition method <🎜>
The method starts with the <<< symbol followed by a custom string, and the last line ends with the custom string and must be in a box. <🎜>
<🎜><🎜>
2. String connection<🎜>
<🎜> Strings can be connected using the string concatenation character (.), such as: <🎜>
A common use is to create large blocks of HTML string code. The assignment symbol (=) and the connector (.) can be abbreviated and combined into the (.=) symbol
Number, such as:
$foo = 2;
echo "foo is $foo"; // 打印结果: foo is 2
echo 'foo is $foo'; // 打印结果: foo is $foo
echo "foo is $foon"; // 打印结果: foo is 2 (同时换行)
echo 'foo is $foon'; // 打印结果: foo is $foon
This feature allows you to join a large number of simple strings without using concatenation symbols. PHP allows us to include the word
directly in a double quoted string
String variables, we can find that the processing results of the following two strings are the same.
Single quote strings and double quote strings are processed differently in PHP. The content in double-quoted strings can be interpreted and replaced, while single-quoted
The contents of the number string are always considered ordinary characters. For example:
代码如下
复制代码
echo "value = $foo";
echo "value = $a[$i]";
The code is as follows
Copy code
$foo = 2;
echo "foo is $foo"; // Print result: foo is 2
代码如下
复制代码
echo "value = $a[$i][$j]"; //我们希望打印二维数组$a的某个元素。
echo 'foo is $foo'; // Print result: foo is $foo
echo "foo is $foon"; // Print result: foo is 2 (with newline)
echo 'foo is $foon'; // Print result: foo is $foon
As you can see, even the backslash within a single-quote string loses its extended meaning (except for the insertion of a backslash and the insertion of a single quote '). Therefore, when you want to perform variable substitution and include escape sequences such as n (newline character) in a string, you should use double quotes
Number. Single quote strings can be used anywhere else. It will be faster to use single quote strings in scripts because the PHP parser is sensitive to
The processing method of single quotation mark string is relatively simple, while the processing of double quotation mark is more complicated because the string also needs to be parsed internally, so the processing speed is
Slightly slower.
Some problems may arise when referencing complex combinations of variables in strings. The following code will work normally:
The code is as follows
Copy code
echo "value = $foo";
echo "value = $a[$i]";
But the following code cannot get the results we want:
The code is as follows
Copy code
echo "value = $a[$i][$j]"; //We want to print an element of the two-dimensional array $a.
To avoid these potential problems in using strings, we usually separate complex variables from strings, like this:
The code is as follows
Copy code
代码如下
复制代码
echo 'value = ' . $a[$i][$j];
echo 'value = ' . $a[$i][$j];
Another way is to enclose complex variables in curly braces, so that the parser can correctly identify them:
代码如下
复制代码
echo "value = {$a[$i][$j]}" //打印二维数组$a的某个元素
The code is as follows
Copy code
echo "value = {$a[$i][$j]}" //Print an element of the two-dimensional array $a
In this way, a new problem arises. When we want to quote the curly brace character itself in a string, we need to remember to use the escape character:
echo "value = {$var}"; // print result "value = {3}"
3. Slashes and SQL statements
Generating HTML code or SQL query statements is often encountered when writing PHP programs and is an interesting thing. Why do you say that?
Because this involves generating another type of code, you must carefully consider and follow the syntax and conventions required for writing this code
代码如下
复制代码
select * from users where last_name = 'O'Keefe'
but.
Let’s look at an example. If you want to query the user whose name is “O’Keefe” in the database, the usual SQL statement is in the form
It goes like this:
代码如下
复制代码
$last_name = "O'Keefe";
$sql = "select * from users where last_name = '" . addslashes($last_name) . "'";
The code is as follows
Copy code
select * from users where last_name = 'O'Keefe'
Please note that the English possessive character (apostrophe) of the SQL statement needs to be escaped with a backslash. PHP provides some functions to handle this
代码如下
复制代码
$sql = 'select * from users where last_name = '' . addslashes($last_name) . ''';
In this case, the purpose of the function AddSlashes($str) is to automatically insert the backslash escape character for the quote character in the string:
The code is as follows
Copy code
$last_name = "O'Keefe";
$sql = "select * from users where last_name = '" . addslashes($last_name) . "'";
In this example, you also need to enclose the last_name string in single quotes (required by SQL syntax), since double is used here.
A string of quotes, so there is no need to escape the pair of single quotes. The following statement is the equivalent of using a single quote string:
The code is as follows
Copy code
$sql = 'select * from users where last_name = '' . addslashes($last_name) . ''';
Anytime you write a string into a database, you have to make sure that the quotes inside are properly escaped, which is a lot of PHP
Common mistakes made by beginners.
4. Double quotes and HTML
Unlike SQL statements, double quotes are often used to represent strings in standard HTML language (many browsers now have strong fault tolerance capabilities
Yes, it is allowed to use single quotes or even no quotes to represent strings in HTML), for example:
HTML language does not support backslash escaping, which will happen when we use hidden inputs of the form to transmit data
Got it. The best way to set the value of hidden inputs is to use the htmlspecialchars() function to encode it. The following statements can be
代码如下
复制代码
To transmit data that may contain double quotes normally:
The code is as follows
Copy code
1. Quotes define strings. To achieve the purpose of including quotation marks, the parser must ignore its original meaning when encountering ordinary quotation marks in the string. We add a backslash in front of the quotation mark to tell PHP: This quotation mark is part of the string and is the correct representation. The method is this: single quote strings can be used anywhere else. The processing speed of using single quote strings in scripts will be faster, because the PHP parser processes single quote strings in a relatively simple way, while the processing of double quotes is due to the internal nature of the string. It also requires parsing, so it's more complex, so the processing speed is slightly slower.
This...double quotes are escaped, single quotes are not escaped
For example: /r/n is a newline, but if you write a file with single quotes, it will not be a newline, but a character. If you write a file with double quotes, it will be a newline.
Finally, check out what other websites have to say
” ” Fields enclosed in double quotes will be interpreted by the compiler and then output as HTML code.
‘ ‘ The words in the single quotes are not interpreted and are output directly.
It can be seen from the literal meaning that single quotes are faster than double quotes.
代码如下
复制代码
$abc=’my name is tome’;
echo $abc //结果是:my name is tom
echo ‘$abc’ //结果是:$abc
echo “$abc” //结果是:my name is tom
For example:
The code is as follows
Copy code
$abc='my name is tome';
echo $abc //The result is: my name is tom
echo ‘$abc’ //The result is: $abc
代码如下
复制代码
select * from abc_table where user_name=’abc’;
echo “$abc” //The result is: my name is tom
Especially when using MYSQL statements, the usage of double quotes and single quotes can be confusing to novices. Here, I will give an example to illustrate.
Assume that constants are used in the query conditions, for example:
The code is as follows
Copy code
select * from abc_table where user_name=’abc’;
SQL statement can be written as:
The code is as follows
代码如下
复制代码
SQLstr = “select * from abc_table where user _name= ‘abc’” ;
Copy code
代码如下
复制代码
$user_name = $_REQUEST['user_name']; //字符串变量
SQLstr = “select * from abc_table where user _name= ‘abc’” ;
1:”select * from table where user_name = ‘ ” //固定SQL语句
2:$user //变量
3:” ‘ ”
1,2,3部分字符串之间用”.”
or
The code is as follows
Copy code
$user=array ("name"=> $_REQUEST['user_name‘,"age"=>$_REQUEST['age'];//Array variableThe SQL statement can be written as:
The code is as follows
Copy code
SQLstr = “select * from abc_table where user_name = ‘ . $user_name . ” ‘ “;
SQLstr = “select * from abc_table where user_name = ‘ ” . $user["name"] . ” ‘ “;
Compare:
The code is as follows
Copy code
SQLstr=”select * from abc_table where user_name = ‘ abc ‘ ” ;
SQLstr=”select * from abc_table where user_name =’ ” . $user _name . ” ‘ “;
SQLstr=”select * from abc_table where user_name =’ ” . $user["name"] . ” ‘ “;
SQLstr can be decomposed into the following 3 parts:
http://www.bkjia.com/PHPjc/632232.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632232.htmlTechArticleThe article uses a large number of implementations to introduce the usage and difference between single quotes and double quotes. Students who need to know more You can refer to this article carefully. In PHP, usually one character...
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn