Detailed description of the difference between single and double quotes in PHP _php tutorial

Source: Internet
Author: User
The article introduces in detail about the differences between single and double quotes in PHP, and the students who need to know can refer to them.

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

$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" the "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.

$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

$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 (simultaneous line wrapping)

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 (simultaneous line wrapping)

Echo ' foo is $foon '; Print Result: Foo is $foon

As you can see, even the backslash in the single quote string loses his extended meaning (except for inserting backslashes \ and inserting 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 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

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

$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:

$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:

$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

http://www.bkjia.com/PHPjc/629102.html www.bkjia.com true http://www.bkjia.com/PHPjc/629102.html techarticle The article introduces in detail about the differences between single and double quotes in PHP, and the students who need to know can refer to them. 1, define the string in PHP, the definition of the string can ...

  • 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.