The difference between PHP single quotes and double quotes_PHP Tutorial
WBOYOriginal
2016-07-21 15:42:54812browse
1. Define a string
In PHP, the definition of a string can use single quotes or double quotes. However, the same single or double quotation marks must be used to define the string. For example, 'Hello' and 'Hello' are illegal string definitions. When defining a string, only one kind of quotation mark is considered as the delimiter, that is, single quotation mark or double quotation mark. Thus, if a string begins with a double quote, only the double quote is parsed by the parser. This way, you can include any other character, even single quotes, within the double-quoted string. The following quotation mark strings are legal: Php code
Copy code The code is as follows:
$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';
Why doesn't "this" work?" will be divided into three sections. If you want to express double quotes in this string, you can use the escape character "" (backslash) to become "Why doesn't "this" work?"
2. Single and double quotation marks in string variables
PHP allows us to directly include string variables in double quotation mark strings. We can find the following two The result of string processing is the same.
Single quote strings and double quote strings are processed differently in PHP. The contents of a double-quoted string can be interpreted and replaced, while the contents of a single-quoted string are always considered ordinary characters. For example: Php code
Copy code The code is as follows:
$foo = 2; echo "foo is $foo"; // Print result: foo is 2 echo 'foo is $foo'; // Print result: foo is $foo echo "foo is $foon"; // Print result: foo is 2 (line feed at the same time) echo 'foo is $foon'; // Print result: foo is $foon $foo = 2; echo "foo is $foo"; // Print result: foo is 2 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 in a single quote string loses its extended meaning (except Insert backslash \ and insert single quote '). Therefore, when you want to perform variable substitution and include escape sequences such as n (newline) in a string, you should use double quotes. 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 also needs to be parsed inside the string. It is therefore more complex and therefore slightly slower to process. Some problems may arise when referencing complex combinations of variables in strings. The following code will work fine: Php code
The following code cannot get the results we want: 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: echo 'value = ' . $a[$i][$j];/ / Use dots (.) to connect strings Another way is to enclose complex variables in curly braces, so that the syntax analyzer can correctly identify them: 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: Php code
This is a problem that is often encountered. The SQL statement inserted into the database uses single quotes to define strings. If you want Inserting a string containing single quotes into the database will cause an error in this SQL statement. For example: $sql="insert into userinfo (username,password) Values('O'Kefee','123456')" At this time, one of the methods is to add escape characters to the SQL statement Backslash, is:...Values('O'Kefee',... Of course, you can also use the function addslashes(), the function of this function is to add escape characters, is: $s = addslashes("O'Kefee") ...Values('".$s."',... Another method is to set the magic-quotes option in php.ini. Turn on this option and pass the form If there are single quotes in the submitted information, escape characters will be automatically added, so there is no need to use other functions. Additional: Let’s start with the functions of double quotes and single quotes: The fields will be interpreted by the compiler and then output as HTML code, but the ones inside the single quotes do not need to be interpreted and will be output directly. For example:
Copy the code The code is as follows:
$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
So when assigning values to SQL statements in the database, they should also be used in double quotes SQL="select a, b,c from ..." But there will be single quotes in the SQL statement to quote the field name
For example: select * from table where user='abc'; The SQL statement here can be written directly as SQL=" select * from table where user='abc'" But if it is like the following:
Copy the code The code is as follows:
$user='abc';
SQL1="select * from table where user=' ".$user." ' "; Compare SQL2="select * from table where user=' abc ' "
I added a little more space between the single quotes and double quotes, I hope you can see it more clearly. That is, replace 'abc' with '".$user."' all within a single quote. Just split the entire SQL string. SQL1 can be decomposed into the following three parts 1: "select * from table where user=' " 2: $user 3: " ' " Use . to connect the strings. So you can understand.
1. Quotes define strings
In PHP, usually a string is defined in a pair of quotation marks, such as: 'I am a string in single quotes' "I am a string in double quotes" The PHP parser uses pairs of quotation marks to judge a string. Therefore, all strings must use the same single or double quotes to define the start and end. For example, the following string definition is illegal: "I am not a valid string since I have unmatching quote marks' 'Me neither!" When defining a string, only one type of quotation marks is used Treated as a delimiter, i.e. single or double quotes. Thus, if a string begins with a double quote , then only the double quotes will be parsed by the parser. This way, you can include any other character within the double-quoted string, even the single-quote symbol. The following quote strings are legal: $s = "I am a 'single quote string' inside a double quote string"; $s = 'I am a "double quote string" inside a single quote string'; When PHP encounters a quote corresponding to the beginning of the string, it thinks it has reached the end of the string, so: "Why doesn't "this" work?" It is actually used The PHP syntax parser is divided into three parts: "Why doesn't " - a double quote string containing a single quote this - extra characters that the parser cannot process " work?" — — Ordinary string The above example attempts to include double quotes within a double quote string, and the parser considers the string to end when it encounters the second double quote . 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 before the quotation mark to tell PHP: this quotation mark is part of the string. The correct representation is this: "Why doesn't "that" work?" A common problem in English strings is the use of apostrophe ', because it is a single quote, and in English (English possessive case) is very common in strings. You have to be careful with these characters: 'You'd better escape your apostrophes' You can see that backslash has its special meaning in strings, when we need to include the backslash itself in the string When , you need to add an extra backslash before the symbol. For example: $file = "c:windowssystem.ini"; echo $file; // The print result is: c:windowssystem.ini $file = "c:\windows\system.ini" ; echo $file; // The print result is: c:windowssystem.ini Another way to define strings, which can eliminate the trouble of special characters and make it easier to quote longer texts. The string definition 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: $first_name = 'Charlie'; $last_name = 'Brown'; $full_name = $first_name . ' ' . $last_name; A common use is to create large blocks of HTML string code, assignment signs (=) and connectors (. ) can be abbreviated and combined into the (.=) symbol , such as: $html = '
This feature allows you to avoid using connections symbols to glue together large numbers of simple strings. PHP allows us to directly include the word string variable in a double-quoted string. 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"; Single quote strings and double quote strings are processed differently in PHP. The content in the double-quoted string can be interpreted and replaced, while the content in the single-quoted string is always considered to be ordinary characters. For example: $foo = 2; echo "foo is $foo"; // Print result: foo is 2 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 Yes, 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 .Single quote strings can be used anywhere else. The processing speed of using single quote strings in scripts will be faster, because the PHP syntax analyzer processes single quote strings in a relatively simple way, while the processing of double quotes is also complicated inside the string. It needs to be parsed, so it is more complicated, 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: echo "value = $foo"; echo "value = $a[$i ]"; The following code cannot get the results we want: echo "value = $a[$i][$j]"; //We want to print a certain part of the two-dimensional array $a element. To avoid these potential problems in using strings, we usually separate complex variables from strings, like this: 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]}" // 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 the string, we need to remember to use the escape character: $var = 3; echo "value = {$var}"; // Print the result "value = 3" 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 I say this? Because it involves generating another type of code, and you must carefully consider and follow the writing syntax and rules required by this type of code. Let’s take a look at an example. If you want to query the user whose name is "O'Keefe" in the database, the usual form of the SQL statement is like this: 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 such situations. The function AddSlashes($str) is used to automatically insert backslash escape characters for quotation mark characters in strings: $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 ( SQL syntax requirements), since the double quotation mark string is used here, there is no need to escape the pair of single quotation marks. The following statement is the equivalent of using a single quoted string: $sql = 'select * from users where last_name = '' . addslashes($last_name) . '''; Any time you want to add a string in a database When writing a string in a string, you must ensure that the quotes inside use escape symbols correctly. This is a common mistake made by many PHP beginners.
4. Double quotation marks and HTML
Unlike SQL statements, double quotation marks are often used to represent strings in standard HTML language (many browsers now have Strong fault tolerance , allowing single quotes or even no quotes to represent strings in HTML), for example: $html = ''. $link.''; $html = "$link"; HTML language does not support backslash escaping. This will be experienced when we use the hidden inputs of the form to transmit data. The best way to set the value of hidden inputs is to use the htmlspecialchars() function to encode it. The following statement can normally transmit data that may contain double quotes:
1. Quotation marks 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 newline Characters, if written into a file with double quotes, are newlines. Agreed.
http://www.bkjia.com/PHPjc/320827.html
www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320827.htmlTechArticle1. Define a string In PHP, you can use single quotes or double quotes to define a string. But the same type of single or double quotes must be used to define the string, such as: 'Hello" and...
Statement:
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