In a programming language, either
single quotation marksStill is
Double quotes, all have a very important role in
PHPLanguage is the same. Compared to ASP, PHP quotation marks better use, the following for you to introduce the difference between single and double quotation marks.
One, a quotation mark defines a string.
To achieve the purpose of including quotation marks, the parser must ignore its original intent when it encounters a string of ordinary quotes, and we will precede the quotation mark with a backslash to tell PHP that the quotation marks are part of the string, and the correct expression is this: the single quote string can be used in more relevant places, Script scripts (batch files) using single-quote string processing speed reading will be faster, should PHP syntax parser for single-quote string processing method is relatively simple, and double-quote processing because the string inside also need to parse, so more complex, so processing speed slightly slower.
This double quotation mark is escaped, and the single quotation mark is not escaped. In PHP, typically 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 quotes to determine a string. Therefore, all strings must use the same single or double quotation mark to define the start and end. For example, the following string definitions are not valid:
- "I am not a valid string since I had unmatching quote marks '
- ' Me neither! '
When defining a string, only one quotation mark is considered a definition, that is, single or double quotation marks. Then, if a string starts with double quotation marks, only the double quotation marks are parsed by the parser. In this way, you can include any more related characters, even single quotes, in a double-quote string. The following string of quotes is valid:
- $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 the quotation marks corresponding to the beginning of the string, it is considered to be at the end of the strings, so:
- "Why doesn ' t" This "Work?"
is actually divided into three parts by the PHP parser:
"Why doesn ' t"--a double quote string containing a single quotation mark
this--extra characters, the parser can't handle it.
"Work?"--ordinary string
The case above attempts to enclose double quotation marks in a double-quote string, and the parser considers the string to end when it encounters the second double quotation mark. To achieve the purpose of including quotation marks, the parser must ignore its original intent when it encounters a string of ordinary quotes, and we'll precede the quotation mark with a backslash to tell PHP that this quotation mark is part of the string, and the correct way to express it is:
- Why doesn ' t ' so ' work? '
A common problem in English strings is the use of apostrophes, which should be a single quotation mark, which is very common in English strings. You must handle these characters with care:
- ' You ' d better escape your apostrophes '
You can see that the backslash has his special meaning in the string, and when we need to include the backslash itself in the string, we need to add a backslash before the symbol. For example:
- $file"C:windowssystem.ini";
- echo$file//Print Result: C:windowssystem.ini
- $file"C:\Windows\System.ini";
- echo$file
Another way to define a string is to eliminate the annoyance of special characters and to make it easier to refer to longer text. The string defines the method to <<<>< p=""><>
Second, the string of links
Strings can use string links (.) To link, such as:
- $first _name' Charlie ';
- $last _name' Brown ';
- $full _name$first _name"$last _name ;
The common use is to create chunks of HTML string source code, the assignment number (=) Link (.) can be abbreviated to the (. =) symbol, such as:
- $html = ';
- $html .= '
;
- for ( $i=0; $i <10; $i ++) {
- $square = $i * $i ;
- $html .= '
;
- }
- $html .= '
'
Number |
Square |
'
' . $i . ' |
' . $square . ' |
'
';
Iii. using variables in strings
This feature allows you to stick to a lot of simple strings without using link symbols. PHP allows us to include string variables directly in a double quote string, and we can see that the following two strings are processed in the same way.
- $full _name$first _name"$last _name;
- $full _name"$first _name $last _name";
The processing of single and double quotation marks in PHP is not the same. The contents of a word in a double-quote string can be parsed and replaced, and the contents of the words in a single quote string are always considered 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 (line wrapping)
- echo' foo is $foon '
As you can see, even the backslash in the single quote string loses his extended meaning (in addition to adding backslashes \ and adding single quotes '). So, you should use double quotes when you want to do variable substitution in strings and escape sequences that contain n (newline characters).
Single quote string can be used in more relevant anywhere, script scripts (batch file) using single quotation string processing speed reading will be faster, should be PHP syntax parser for single-quote string processing method is relatively simple, and double-quote processing because the string inside also need to parse, so more complex, so processing speed slightly slow.
When referencing a complex combination of variables in a string, some problems may arise, and the following source code will work correctly:
- echo"value = $foo";
- echo"value = $a [$i]";
The following source code does not get the result we want:
- echo"value = $a [$i] [$j]"
To avoid the potential problems in the use of these strings, we often separate complex variables from strings, like this:
- echo' value = '$a[$i][$j ];
Another way to do this is to enclose the complex variable in curly braces, which can be correctly identified by the parser:
- echo"value = {$a [$i] [$j]}"
In this way, new problems have arisen. When we want to refer to the curly brace character itself in a string, we'll remember to use the escape character:
- $var = 3;
- echo"value = {$var}"//Print Result "value = 3"
- echo"value = {$var}"
Three, Slash, and SQL statements
Generating HTML source code or SQL query statements is a common and interesting issue when writing PHP programs. Why do you say that? This involves generating another type of source code, and you must carefully consider and follow the writing syntax and rules required by this source code.
Let's look at a case where you want to query the database for users whose names are "O ' Keefe", usually in the form of SQL statements:
- last_name = ' O ' Keefe '
Please indicate that the SQL statement (apostrophe) must be escaped with a backslash. PHP specifically gives a number of functions to handle this situation, the function of Addslashes ($STR) is the purpose of the electronic in the string of quotation mark characters into the backslash escape character:
- $last _name"O ' Keefe";
- $sql"SELECT * from users where last_name = '"addslashes( $last _name "'";
In this case, you will also enclose the single quotation mark (SQL syntax requirement) outside the last_name string, because there is a double quote string, so there is no need to use escape for this pair of single quotes. The following statement is the equivalent of using a single quote string:
- $sql' select * from users where last_name = 'addslashes($last _ Name";
Any time you want to write a string in the database, you have to make sure that the quotes inside are correctly using the escape symbol, which is a common mistake for many PHP beginners.
Four, double quotes and HTML
Unlike SQL statements, double quotes are often used to express strings in the standard HTML language (many browsers now have strong fault tolerance, allowing the use of single quotes in HTML or even without quotes), for example:
- $html'. $url . ' > ' . $link . '' ;
- $html"$link";
The HTML language does not support backslash escaping, which is something that we experience when we use the hidden inputs of the list to transfer data. The best way to set the value of hidden inputs is to encode it using the Htmlspecialchars () function. The following statement can normally transmit a data that may contain double quotes:
- < input type=hiddenname= var value = " " >
Through the introduction of this article, I hope to help you.
http://www.bkjia.com/PHPjc/445809.html www.bkjia.com true http://www.bkjia.com/PHPjc/445809.html techarticle In a programming language, both single and double quotes are important, as is the case in the PHP language. Compared with ASP, PHP quotation marks better use, the following for you to introduce the single citation ...