Describe in detail the difference between single and double quotes in PHP

Source: Internet
Author: User

1. Define String
In PHP, a string can be defined using single quotes or double quotes. However, you must use the same single or double quotes to define the string, such as: ' Hello ' and ' hello ' are illegal string definitions.
When you define a string, only one quotation mark is treated as a definition character, either single or double quotes. So, if a string starts with double quotes, then only double quotes are parsed by the parser. This way, you can include any other character, even single quotes, in the double quote string. The following quotation marks are valid:

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 the string "Why doesn ' t" this "work" will be divided into three paragraphs. If you want to express double quotes in this string, you can use the escape character "" (backslash) to change to "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 have the same processing results.

$full _name = $first _name. ' ' . $last _name;

$full _name = "$first _name $last _name";

Single quote strings and double quote strings are handled differently in PHP. The contents of a double quote string can be interpreted and replaced, and the contents of a single quote string are always considered normal characters. For example:

PHP code

$foo = 2;

echo "Foo is $foo"; Printed Result: Foo is 2

Echo ' foo is $foo '; Printed result: Foo is $foo

echo "Foo is $foon"; Printed Result: Foo is 2 (newline at the same time)

Echo ' foo is $foon '; Printed result: Foo is $foon

$foo = 2;

echo "Foo is $foo"; Printed Result: Foo is 2

Echo ' foo is $foo '; Printed result: Foo is $foo

echo "Foo is $foon"; Printed Result: Foo is 2 (newline at the same time)

Echo ' foo is $foon '; Printed result: Foo is $foon

As you can see, even a backslash in a single quote string loses his extended meaning (except inserting a backslash \ and inserting single quotes '). So, you should use double quotes when you want to make variable substitutions in a string and an escape sequence that contains n (line breaks). Single quote strings can be used anywhere else, and the use of single quote strings in a script is faster because the PHP parser handles single quote strings in a simpler way, and double quotes are more complex because they need to be parsed inside the string, so the processing speed is slightly slower.

When you refer to a complex combination of variables in a string, some problems may arise and the following code works correctly:

PHP code

echo "value = $foo";

echo "value = $a [$i]";

echo "value = $foo";

echo "value = $a [$i]";

The following code does not get the result we want:

echo "value = $a [$i] [$j]"; We want to print an element of the two-dimensional array $a.

To avoid potential problems with these strings, we usually separate complex variables from strings, like this: echo ' value = '. $a [$i] [$j The connection of the];//string with dots (.)

Another option is to enclose complex variables in curly braces, and the parser can correctly identify:

echo "value = {$a [$i] [$j]}"//print an element of a two-dimensional array $a

In this way, there are new problems. When we want to refer to the curly braces character itself in a string, remember to use the escape character:

PHP code

$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 recurring problem, when the SQL statement that inserts the database uses single quotes to define the string, and if you insert a string that contains single quotes into the database, the SQL statement will go wrong.

such as: $sql = "INSERT into UserInfo (Username,password) Values (' O ' kefee ', ' 123456 ')"

At this point, one of the ways to handle this is to add an escape character backslash to the SQL statement.

That is: ... Values (' O ' Kefee ',......

Of course, you can also use the function addslashes (), the function of which is to add an escape character,

namely: $s = addslashes ("O ' Kefee") ... Values (' ". $s." ',......

There is also a way to set the Magic-quotes option in PHP.ini, which opens the option so that if a single quote is in the information submitted by the form, it will automatically be added as an escape character. So you don't have to use other functions.

Add: This is going to start 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 quote doesn't need to be interpreted and directly output.

For example:

$ABC = ' I love u ';

echo $ABC//Result: I love u

The echo ' $abc '//result is: $ABC

echo "$ABC"//Result: I love u

So when you assign a value to a SQL statement inside a database, you also use double quotes inside sql= "Select A,b,c from ..." But there's a single quote in the SQL statement that leads to the field name.

For example: SELECT * FROM table where user= ' abc ';

The SQL statement here can be written directly into the sql= "select * from table where user= ' abc '"

But if it's like the following:

$user = ' abc ';

Sql1= "SELECT * from table where user= '". $user. " "; contrast

Sql2= "SELECT * from table where user= ' abc '"

I've added a little space between the single and double quotes, and I hope you can see it clearly.

Which is to replace ' abc ' with '. $user. ' It's all in one single quote. 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

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.

Tags Index: