The difference between single and double quotes in PHP

  • 2020-03-31 19:29:13
  • OfStack

1. Define a string

In PHP, strings can be defined using either single or double quotes. However, you must use the same single or double quotation marks to define the string, for example, 'Hello' and' Hello' are illegal string definitions.
When defining a string, there is only one type of quote that is considered a delimiter, single or double. Thus, if a string starts with a double quotation mark, only the double quotation mark is parsed by the parser. In this way, you can include any other character in the double quote string, even single quotes. The following quote strings are legal:
The 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 string "Why doesn't "this" work?" It will be divided into three segments. If you want to represent double quotes in this string, you can use the escape "\" (backslash) to make it "Why doesn't this\" this\" work?" Can.

2. Single and double quotes in string variables

PHP allows us to include string variables directly in double-quoted strings, and we can see that the two strings below are treated the same.
 
$full_name = $first_name . ' ' . $last_name; 
$full_name = "$first_name $last_name";    

Single and double quote strings are handled 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 normal characters. Such as:
The Php code
 
$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( At the same time line ) 
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( At the same time line ) 
echo 'foo is $foon'; //Print result: foo is $foon   

As you can see, even backslashes lose their extended meaning in single quote strings (except for inserting backslashes \\ and single quote \'). So, you should use double quotes when you want to make variable substitutions in strings and escape sequences that contain \n (line break). Single quote strings can be used anywhere else. It is faster to use a single quote string in a script because the PHP parser handles single quote strings more simply, and the double quote processing is slower because it is more complex to parse inside the string.
Some problems may arise when referring to complex combinations of variables in a string, and the following code works fine:
The Php code
 
echo "value = $foo"; 
echo "value = $a[$i]"; 
echo "value = $foo"; 
echo "value = $a[$i]";    

The following code does not get the results we want:
Echo "value = $a [$I] [$j]". // we want to print some element of the two-dimensional array $a.
To avoid potential problems in string usage, we usually separate complex variables from the string like this: echo 'value = '. $a[$I][$j]; // string connection with the point (.)
An alternative is to enclose complex variables in curly braces so that the parser can recognize them correctly:
Echo "value = {$a[$I][$j]}" // print an element of the two-dimensional array $a
So, a new problem arises. When we want to reference the curly brace character itself in a string, remember to use an escape:
The Php code
 
$var = 3; 
echo "value = {$var}"; //Print the result "value = 3"
echo "value = {$var}"; //Print result "value = {3}"
$var = 3; 
echo "value = {$var}"; //Print the result "value = 3"
echo "value = {$var}"; //Print result "value = {3}"


3. In SQL statements

This is a common problem, in the insert database SQL statement is to use single quotes to define the string, if you want to insert a string containing single quotes into the database, this SQL statement will be wrong.
$SQL ="insert into userinfo (username,password) Values('O'Kefee','123456')"
At this point, one way to handle this is to add an escape backslash to the SQL statement,
That is:... Values (' O \ 'Kefee',...
You can also use the function addslashes(), which adds an escape,
$s = addslashes("O 'kefee ")... Values (' ". $s. "',...
Another option is to set the magic-quotes option in php.ini. If you turn it on, the single quotes in the information submitted through the form will be added automatically, such as escape. So you don't have to use any other functions.
Note: this starts with double and single quotes: fields in double quotes are interpreted by the compiler and output as HTML code, but fields in single quotes are output without explanation.
Such as:
 
$abc='I love u'; 
echo $abc //The result :I love u
echo '$abc' //The result is: $ABC
echo "$abc" //The result :I love u

SQL="select a,b,c from..." But there are single quotes in the SQL statement that take out the field name
Select * from table where user=' ABC ';
The SQL statement here can be written as SQL="select * from table where user=' ABC '"
But if like the following:
 
$user='abc'; 
SQL1="select * from table where user=' ".$user." ' "; Compare the  
SQL2="select * from table where user=' abc ' " 

I put a little more space between the single and double quotes, just to make it clear.
So I'm just going to replace 'ABC' with '.$user. 'in a single quote. I just split the entire SQL string. SQL1 can be broken down into the following three parts
1: "select * from table where user='"
2: $user
3: "'"
Strings are concatenated with., that makes sense.  

Quotes define strings

In PHP, a string is usually defined in a pair of quotes, 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
Quotes define the beginning and end. For example, the following string definition is illegal:
"I am not a valid string since I have unmatched quote marks'
'Me neither!"
When defining a string, there is only one type of quote that is considered a delimiter, single or double. So, if a string has a double argument
At the beginning, only the double quotes are parsed by the parser. This way, you can include any other character, even a single quote, in the double quote string
Number. 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 quotation mark corresponding to the beginning of a string, it thinks it has reached the end of the string.
"This", "according to doesn 't work?"
It's actually divided into three parts by the PHP parser:
"Why doesn't it "-- a string of double quotes that contains a single quote
This - extra characters that the parser cannot handle
"Work?" -- normal string
The above example attempts to include a double quotation mark in a double quotation mark string, and the parser considers a string knot when it encounters a second double quotation mark
The beam. To achieve the goal of including quotes, the parser must ignore the original meaning of a string when it encounters a normal quote
A backslash tells PHP that the quote is part of the string.
"According to doesn 't \" that \ "work?"
A common problem in English strings is the use of the apostrophe 'because it is a single quotation mark, which is very common in English strings
(English possessive). You must be careful with these characters:
'You'd better escape your apostrophes'
You can see that the backslash has a special meaning in the string, when we need to include the backslash itself in the string, we need to
This symbol is preceded by an extra backslash. Such as:
$file = "c: \ Windows \ system. Ini".
Echo $file; // the printed result is: c: Windows system.ini
$file = "c: \ \ Windows \ \ system. Ini".
Echo $file; // the printed result is: c:\ Windows \system.ini
Another way to define strings that eliminates the need for special characters and makes it easy to reference longer text. The string defines the square
Method to < < < The symbol begins immediately after a custom string, and the last line ends with that custom string and must be capped.

Two, string connection

Strings can be connected using a string connector (.), such as:
$first_name = 'Charlie';
$last_name = 'Brown';
$full_name = $first_name. "". $last_name;
A common use is to create large chunks of HTML string code, and the assignment number (=) concatenation (.) can be shortened to (.=)
, such as:
= '$HTML < Table> ';
$HTML. = '< Tr> < Td> Number< / td> < Td> Square< / td> < / tr> ';
For ($I = 0; $i< 10; ${i++)
$square = $I * $I;
$HTML. = '< Tr> < Td> '. $I. '< / td> < Td> '. $square. '< / td> < / tr> ';
}
$HTML. = '< / table> ';

Use variables in strings

This feature allows you to glue large Numbers of simple strings without using concatenation symbols. PHP allows us to include words directly in double quote strings
String variables, we can find the following two string processing results are the same.
$full_name = $first_name. "". $last_name;
$full_name = "$first_name $last_name";
Single and double quote strings are handled differently in PHP. The contents of the double quote string can be interpreted and replaced while the single quote
The contents of a string are always considered normal characters. Such as:
$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 (also line feed)
Echo 'foo is $foo \ n'; // print result: foo is $foo\n
As you can see, even backslashes lose their extended meaning in single quote strings (except for inserting backslashes \\ and inserting single
Quotation marks \'). So, you should use double quotes when you want to make a variable substitution in a string and to include escape sequences such as \n (line break)
Number. Single quote strings can be used anywhere else, and it's faster to use them in scripts because of the PHP parser pair
Single quotation marks are handled in a simple way, while double quotation marks are handled in a more complicated way due to the internal parsing of the string, so the processing speed is higher
A little slow.
Some problems may arise when referring to complex combinations of variables in a string, and the following code works fine:
Echo "value = $foo";
Echo "value = $a [$I]".
The following code does not get the results we want:
Echo "value = $a [$I] [$j]". // we want to print some element of the two-dimensional array $a.
To avoid potential problems with these strings, we usually separate complex variables from the strings, like this:
Echo 'value = '. $a[$I][$j];
An alternative is to enclose complex variables in curly braces so that the parser can recognize them correctly:
Echo "value = {$a[$I][$j]}" // print an element of the two-dimensional array $a
So, a new problem arises. When we want to reference the curly brace character itself in a string, remember to use an escape:
$var = 3;
Echo "value = {$var}"; // print result "value = 3"
Echo "value = \ {$var}"; // print result "value = {3}"

Slashes and SQL statements

Generating HTML code or SQL queries 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 coding syntax and rules required by this type of code
Then.
Let's take a look at an example where you want to query a database for a user whose name is "O 'keefe," usually in the form of an SQL statement
Here it is:
Select * from users where last_name = 'O\'Keefe'
Note that the English possessive (apostrophe) of the SQL statement is escaped using a backslash. PHP specifically provides some functions to handle this
In this case, the function AddSlashes($STR) is used to automatically insert backslash escape characters into the string:
$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 requires), since you use double
The string of quotes, so there is no need to escape the pair of single quotes. The following statement is the equivalent of a single quote string:
$SQL = 'select * from users where last_name = \''. Addslashes ($last_name). '\';
Any time you want to write a string in a database, you have to make sure that the quotes inside are properly escaped, which is a lot of PHP
A common mistake made by beginners.

Four, double quotes and HTML

Unlike SQL statements, double quotes are often used to represent strings in standard HTML (many browsers now have strong fault tolerance)
Yes, allow single quotes or even no quotes to represent strings in HTML), for example:
= '$HTML < A href = "'. $url. '" > '. $link. '< / a> ';
$HTML = "< A href = \ "$url \" > $link< / a>" ;
The HTML language does not support backslash escape, which is useful when transmitting data using the form's hidden inputs
The experience. The best way to set the value of hidden inputs is to use htmlspecialchars() to encode them. The following statement can be
To normally transmit data that might contain double quotes:
< Input type = hidden name = var value = "< ? PHP echo htmlspecialchars ($var)? >" >

Quotes define strings. To achieve the goal that contains quotes, you must analyzer to meet a bunch of ordinary quotes in ignore its original meaning, we a backslash in front of the quotation marks to tell PHP: this quote is part of the string, would be the right way to said like this: single quotation marks can be used in any other place, the script using single quotes string processing speed will be faster, because PHP parser for single string processing method is simple, and the processing of double quotes because internal string need to parse, therefore more complex, so the processing speed is slightly slow.

This... Double quotes escape, single quotes do not escape
For example, /r/n is a new line, but if you write a file in single quotes, it's not a new line, it's a single character, if you write a file in double quotes, it's a new line.
Agree with you.

Related articles: