1. Defining Strings
In PHP, the definition of a string can use either single quotation marks or double quotes. However, you must use the same single or double quotation marks to define strings, such as: ' Hello ' and ' hello ' are illegal string definitions.
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 quotes, only the double quotes are parsed by the parser. This way, you can include any other characters, even single quotes, in a double-quote string. The following string of quotes is valid:
PHP code
Copy CodeThe 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 ';
And the string "Why doesn's" this "work?" will be divided into three paragraphs. If you want to represent double quotes in this string, you can use the escape character "\" (backslash) to become "why doesn ' t \" this\ "work?".
2. Single and double quotes in string variables
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.
Copy CodeThe code is as follows:
$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 double-quote string can be interpreted and replaced, and the contents of a single quote string are always considered ordinary characters. For example:
PHP code
Copy CodeThe 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 $foo \ n"; Print Result: Foo is 2 (simultaneous line wrapping)
Echo ' foo is $foo \ n '; Print Result: Foo is $foo \ n
$foo = 2;
echo "Foo is $foo"; Print Result: Foo is 2
Echo ' foo is $foo '; Print Result: Foo is $foo
echo "Foo is $foo \ n"; Print Result: Foo is 2 (simultaneous line wrapping)
Echo ' foo is $foo \ n '; Print Result: Foo is $foo \ n
As you can see, even the backslash in the single quote string loses his extended meaning (except for inserting backslashes \ \ and inserting single quotation marks \ '). Therefore, you should use double quotation marks when you want to perform variable substitution in a string and include escape sequences such as \ n (line break). Single quote string can be used anywhere else, the script using single-quote string processing speed is faster, because the PHP parser on the 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.
When you reference a complex combination of variables in a string, some problems may arise, and the following code will work correctly:
PHP code
Copy CodeThe code is as follows:
echo "value = $foo";
echo "value = $a [$i]";
echo "value = $foo";
echo "value = $a [$i]";
And the following code does not get the result we want:
echo "value = $a [$i] [$j]"; We want to print an element of a two-dimensional array of $ A.
To avoid the potential problems in the use of these strings, we often separate complex variables from the string, like this: echo ' value = '. $a [$i] [$j];//string Connection point (.)
Another option is to enclose the complex variable in curly braces, which the parser can correctly identify:
echo "value = {$a [$i] [$j]}"//prints an element of two-dimensional array $ A
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:
PHP code
Copy CodeThe code is as follows:
$var = 3;
echo "value = {$var}"; Print result "value = 3"
echo "value = \{$var}"; Print Result "value = {3}"
$var = 3;
echo "value = {$var}"; Print result "value = 3"
echo "value = \{$var}"; Print Result "value = {3}"
3. In the SQL statement
This is a common problem, the SQL statement inserted in the database is a single quotation mark to define the string, if you want to insert a string containing single quotation marks into the database, this SQL statement will be an error.
such as: $sql = "INSERT into UserInfo (Username,password) Values (' O ' kefee ', ' 123456 ')"
At this point, one of the ways to do this is to include an escape character backslash in the SQL statement,
That is: ... Values (' o\ ' Kefee ',......
Of course, you can also use the function addslashes (), the function is to add the escape character,
That is: $s = addslashes ("O ' Kefee") ... Values (' ". $s." ',......
Another way is to set the Magic-quotes option in php.ini to open this option, and if there is a single quotation mark in the information submitted through the form, it will be automatically added as an escape character. So no other functions are used.
Add: This starts with double quotes and single quotes: The fields inside the double quotes are interpreted by the compiler and then exported as HTML code, but the single quotes do not need to be interpreted and output directly.
For example:
Copy CodeThe code is as follows:
$ABC = ' I love u ';
echo $ABC//result is: I love u
Echo ' $ABC '//Result: $ABC
echo "$ABC"//result is: I love u
So when you assign a value to a SQL statement inside a database, use double quotes inside the sql= "select A,b,c from ..." But there are single quotes in the SQL statement that enclose the field names
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 looks like the following:
Copy CodeThe code is as follows:
$user = ' abc ';
Sql1= "SELECT * from table where user= '". $user. " "; compare
Sql2= "SELECT * from table where user= ' abc '"
I have added a little space between the single and double quotation marks, and I hope you can see it clearly.
That is, replace ' abc ' with '. $user. ' Are all in a single quotation mark. Just split the entire SQL string. SQL1 can be decomposed into the following 3 parts
1: "SELECT * from table where user= '"
2: $user
3: "'"
Used between strings. To connect, so you can understand.
One, the quotation mark defines the string
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 marks 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. So if a string is represented by a double-cited
Number, then only double quotes are parsed by the parser. In this way, you can include any other character in a double-quote string, or even a single-cited
No. 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, parser cannot process
"Work?"--ordinary string
This example attempts to include double quotation marks in a double-quote string, and the parser considers the string knot when it encounters the second double quotation mark.
A bundle. To achieve the purpose of containing quotation marks, the parser must ignore the literal quotes in the string, and we
Preceded by a backslash to tell PHP: This quotation mark is part of the string, the correct way to represent it:
"Why doesn ' t \" that\ "work?"
A common problem in English strings is the use of apostrophes because it is a single quotation mark, which is very common in English strings.
(All in English). 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
The symbol is preceded by a backslash. For example:
$file = "C:\Windows\System.ini";
Echo $file; Printing Result: C:windowssystem.ini
$file = "C:\\windows\\system.ini";
Echo $file; Printing Result: C:\Windows\System.ini
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 party
Law to << <符号紧跟一个自定义字符串开头,最后一行以该自定义字符串结束,并且必须顶格。
Second, the connection of strings
Strings can use string connectors (.) To connect, such as:
$first _name = ' Charlie ';
$last _name = ' Brown ';
$full _name = $first _name. ' ' . $last _name;
A common use is to create chunks of HTML string code, and the assignment number (=) connector (.) can be abbreviated to (. =) characters
Numbers, such as:
$html = '
'; $html. = '
Number |
Square |
'; for ($i =0; $i <10; $i + +) {$square = $i * $i; $html. = '
' . $i. ' |
' . $square. ' |
'; } $html. = '
';
iii. using variables in strings
This feature allows you to glue and use a large number of simple strings without using connection symbols. PHP allows us to include words directly in a double quote string
String variable, we can find that the following two string processing results are the same.
$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 the double-quote string can be interpreted and replaced, and the single-citation
The contents of the string are 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 $foo \ n"; Print Result: Foo is 2 (simultaneous line wrapping)
Echo ' foo is $foo \ n '; Print Result: Foo is $foo \ n
As you can see, in a single quote string even the backslash also loses his extended meaning (in addition to inserting a backslash \ \ and inserting a single
quotation marks \ '). So, when you want to do variable substitution in strings and escape sequences that contain \ n (newline characters), you should use double-cited
No. Single-quote strings can be used anywhere else, and it is faster to use single-quote strings in scripts, because the PHP parser
Single-Quote string processing method is relatively simple, and double-quote processing because the string internal also need to parse, so more complex, so the processing speed
Slightly slower.
When you reference a complex combination of variables in a string, some problems may arise, and the following code will work correctly:
echo "value = $foo";
echo "value = $a [$i]";
And the following code does not get the result we want:
echo "value = $a [$i] [$j]"; We want to print an element of a two-dimensional array of $ A.
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 option is to enclose the complex variable in curly braces, which the parser can correctly identify:
echo "value = {$a [$i] [$j]}"//prints an element of two-dimensional array $ A
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}"; Print Result "value = {3}"
three, Slash, and SQL statements
Generating HTML code or SQL query statements is a common and interesting thing to do when writing PHP programs. Why do you say that?
Because this involves generating another type of code, you must carefully consider and follow the syntax and rules required by this Code
The
Let's look at an example where you want to query the database for users whose names are "O ' Keefe", usually in the form of SQL statements
That is true:
SELECT * from users where last_name = ' o\ ' Keefe '
Note that the SQL statement, the English-language (apostrophe), must be escaped with a backslash. PHP specifically provides some functions to handle such
, the purpose of the function addslashes ($STR) is to automatically insert a backslash escape character in a string with the quote character:
$last _name = "O ' Keefe";
$sql = "SELECT * from users where last_name = '". Addslashes ($last _name). "'";
In this example, you will also enclose the single quotation mark (SQL syntax requirement) outside the last_name string, as this is used for dual
Quoted string, so no escaping is necessary 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 lot of PHP
Mistakes that beginners often make.
four, double quotes and HTML
Unlike SQL statements, double quotes are often used to denote strings in standard HTML languages (many browsers now have strong fault-tolerant
Can, allow single quotes in HTML or even without quotation marks to denote strings), for example:
$html = ". $link.";
$html = "$link";
The HTML language does not support backslash escaping, which is useful when we use the form's hidden inputs to transfer data.
Have realized. The best way to set the value of hidden inputs is to encode it using the Htmlspecialchars () function. The following statement can be
To normally transmit a data that may contain double quotes:
One, a quotation mark defines a string. To achieve the purpose of including quotation marks, the parser must ignore the literal quotes in the string, ignoring its original intent, we put a backslash in front of the quotation marks to tell PHP: This quotation mark is part of the string, the correct way to represent: the single quote string can be used anywhere else, It is faster to use single-quote string processing in a script, because the PHP parser handles single-quote strings more simply, and the processing of double-quotes is more complicated because the strings need to be parsed internally, so the processing speed is slightly slower.
This one... Double quotes escaped, single quotes not escaped
such as:/r/n is a newline, but if you write to the file in single quotation marks, it is not a newline, but a character, if the file is written in double quotation marks, it is a newline.
Agree.
http://www.bkjia.com/PHPjc/320827.html www.bkjia.com true http://www.bkjia.com/PHPjc/320827.html techarticle 1. Defining Strings in PHP, the definition of a string can use single quotes or double quotes. However, you must use the same single or double quotation mark to define a string, such as: ' Hello ' and ...