The difference between single quotation marks and double quotes in PHP

Source: Internet
Author: User
The difference between single quotation marks and double quotes in PHP

In a programming language, both single and double quotes are important, as is the case in the PHP language. Compared to ASP, PHP quotation marks better use, in ASP, to put the data into the variable must be enclosed in double quotation marks, and in the case of the use of quotation marks can only be used in single quotes, not double quotation marks, if the use of double quotation marks will be used as the end of the previous quotation mark. However, there is no such restriction in PHP. Substituting variables, single and double quotes can be used, but in pairs.

In PHP, there is no difference between single and double quotes if you are only using text-type data that does not contain variables. But if you want to use a variable, the single and double quotes are different. In PHP, double quotes can be used to directly substitute variables, without the need for a definition or other symbols, such as: $b = "Cat", $a = "thisisa$b";//display Thisisacat single quotation mark is not, if $a= ' thisisa$b ', then display as: thisisa$b.

In the operational efficiency of the single quotation mark and double quotation marks are also different, in general, single quotation marks will run faster, double quotation marks are slower, because the double quotation marks to find whether there are variables in the statement, and single quotation marks are not used, so if there is no substitute variable in the statement as far as possible single quotation marks. This is a habit of writing programs, always thinking about improving the efficiency of the program.

If you want to define an action in a statement, you must use double quotation marks. For example, when you go to define single quotes, it is written like this: $a = ' he\ ' Snameistom. ', the program will show he\ ' snameistom intact, if so write: $a = "he\ ' Snameistom.", The program will show: he ' Snameistom.

Single quotation marks are stumbling blocks to SQL statements, and text data in SQL statements is enclosed in single quotes, so if single quotes appear in the data, the database considers the data to end, then the subsequent data is considered to be the other parts of the SQL statement, which of course will be an error when querying the database. So text-based data written into SQL statements must be addslashes () to define single quotes, and then use Stripslashes () to turn back when reading the data.

From PHP single and double quotes can be seen, PHP language than ASP language with vitality, because it is very flexible, and think very thoughtful, from single quotation marks and double quotation marks of these bits can be seen.

The difference between single and double quotation marks in PHP

In PHP, typically a string is defined in a pair of quotation marks, such as:

' Iamastringinsinglequotes '

"Iamastringindoublequotes"

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:

"Iamnotavalidstringsinceihaveunmatchingquotemarks '

' meneither! '

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 = "Iama ' singlequotestring ' insideadoublequotestring";

$s = ' Iama ' doublequotestring "insideasinglequotestring";

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:

"Whydoesn ' t" this "work?"

is actually divided into three parts by the PHP parser:

"WHYDOESN"--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:

"Whydoesn ' 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\ ' dbetterescapeyourapostrophes '

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;//Print Result: C:windowssystem.ini

$file = "C:\\windows\\system.ini";

echo$file;//Print 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 <<<>< p=""><>

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, assignment number (=) connectors (.). can be abbreviated to (. =) characters

Numbers, 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 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 "Foois$foo";//Print Result: Foois2

Echo ' Foois$foo ';//print Result: Foois$foo

echo "foois$foo\n";//Print Result: Foois2 (simultaneous newline)

Echo ' foois$foo\n ';//print Result: foois$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]}"//print an element of a two-dimensional array of $ 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*fromuserswherelast_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*fromuserswherelast_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*fromuserswherelast_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 hiddeninputs to transfer data.

Have realized. The best way to set the value of hiddeninputs 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.

One, the quotation mark defines the string

In PHP, typically a string is defined in a pair of quotation marks, such as:

' Iamastringinsinglequotes '

"Iamastringindoublequotes"

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:

"Iamnotavalidstringsinceihaveunmatchingquotemarks '

' meneither! '

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 = "Iama ' singlequotestring ' insideadoublequotestring";

$s = ' Iama ' doublequotestring "insideasinglequotestring";

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:

"Whydoesn ' t" this "work?"

is actually divided into three parts by the PHP parser:

"WHYDOESN"--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:

"Whydoesn ' 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\ ' dbetterescapeyourapostrophes '

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;//Print Result: C:windowssystem.ini

$file = "C:\\windows\\system.ini";

echo$file;//Print 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 <<<>< p=""><>

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, assignment number (=) connectors (.). can be abbreviated to (. =) characters

Numbers, 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 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 "Foois$foo";//Print Result: Foois2

Echo ' Foois$foo ';//print Result: Foois$foo

echo "foois$foo\n";//Print Result: Foois2 (simultaneous newline)

Echo ' foois$foo\n ';//print Result: foois$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]}"//print an element of a two-dimensional array of $ 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*fromuserswherelast_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*fromuserswherelast_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*fromuserswherelast_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 hiddeninputs to transfer data.

Have realized. The best way to set the value of hiddeninputs 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.

?

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.