Talk about the difference between single and double quotes in PHP _php example

Source: Internet
Author: User

In PHP, the definition of a string can be in English single quotes ', or you can use double quotes in English.

But you must use the same single or double quotes to define strings, such as ' Hello World ' and ' Hello World ' for illegal string definitions.

What's the difference between single and double quotes? Let's learn from the following article.

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 represent double quotes in this string, you can use the escape character "\" (backslash) to change to "Why doesn" 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: The 2 
echo ' foo is $foo ';//print Result: Foo is $foo 
echo "", $foo \ n ";//Print Result: Foo is 2 (line wrap) 
Echo ' Foo is $foo \ n '; Printed result: the foo is $foo \ n 
$foo = 2; 
echo "Foo is $foo"; Printed Result: The 2 
echo ' foo is $foo ';//print Result: Foo is $foo 
echo "" \ is $foo \ n ";//Print Result: Foo is 2 (line wrap) 
E Cho ' foo is $foo \ n '; Printed result: Foo is $foo \ n

As you can see, even the backslash in the 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 include the escape sequences of \ 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"; 

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 
echo ' $abc '//Result: $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. " ' "; compare 
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, so you can understand.

The above is a small set of PHP to introduce the difference between single and double quotes, I hope to help everyone, if you want to know more content please pay attention to cloud Habitat community website!

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.