Discussion on the difference between single and double quotation marks in PHP programming language _php Tutorial

Source: Internet
Author: User
Tags php programming language vars
In a programming language, either single quotation marksStill is Double quotes, all have a very important role in PHPLanguage is the same. Compared to ASP, PHP quotation marks better use, the following for you to introduce the difference between single and double quotation marks.

One, a quotation mark defines a string.

To achieve the purpose of including quotation marks, the parser must ignore its original intent when it encounters a string of ordinary quotes, and we will precede the quotation mark with a backslash to tell PHP that the quotation marks are part of the string, and the correct expression is this: the single quote string can be used in more relevant places, Script scripts (batch files) using single-quote string processing speed reading will be faster, should PHP syntax parser for 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.

This double quotation mark is escaped, and the single quotation mark is not escaped. In PHP, typically a string is defined in a pair of quotation marks, such as:

 
  
  
  1. ' I am a string in single quotes '
  2. "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 quotation mark to define the start and end. For example, the following string definitions are not valid:

 
  
  
  1. "I am not a valid string since I had unmatching quote marks '
  2. ' Me neither! '

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 quotation marks, only the double quotation marks are parsed by the parser. In this way, you can include any more related characters, even single quotes, in a double-quote string. The following string of quotes is valid:

 
  
  
  1. $s"I am a ' single quote string ' inside a double quote string";
  2. $s' I am a ' double quote string ' inside a single quote string ';

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:

 
  
  
  1. "Why doesn ' t" This "Work?"

is actually divided into three parts by the PHP parser:

"Why doesn ' t"--a double quote string containing a single quotation mark

this--extra characters, the parser can't handle it.

"Work?"--ordinary string

The case above attempts to enclose double quotation marks in a double-quote string, and the parser considers the string to end when it encounters the second double quotation mark. To achieve the purpose of including quotation marks, the parser must ignore its original intent when it encounters a string of ordinary quotes, and we'll precede the quotation mark with a backslash to tell PHP that this quotation mark is part of the string, and the correct way to express it is:

 
  
  
  1. Why doesn ' t ' so ' work? '

A common problem in English strings is the use of apostrophes, which should be a single quotation mark, which is very common in English strings. You must handle these characters with care:

 
  
  
  1. ' You ' d better escape your apostrophes '

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 add a backslash before the symbol. For example:

 
  
  
  1. $file"C:windowssystem.ini";
  2. echo$file//Print Result: C:windowssystem.ini
  3. $file"C:\Windows\System.ini";
  4. echo$file

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 method to <<<>< p=""><>

Second, the string of links

Strings can use string links (.) To link, such as:

 
  
  
  1. $first _name' Charlie ';
  2. $last _name' Brown ';
  3. $full _name$first _name"$last _name ;

The common use is to create chunks of HTML string source code, the assignment number (=) Link (.) can be abbreviated to the (. =) symbol, such as:

 
 
  1. $html = ';
  2. $html .= '
  3. ;
  4. for ( $i=0; $i <10; $i ++) {
  5. $square = $i * $i ;
  6. $html .= '
  7. ;
  8. }
  9. $html .= '
  10. ' ' '
    Number Square
    ' . $i . ' ' . $square . '
    ';

Iii. using variables in strings

This feature allows you to stick to a lot of simple strings without using link symbols. 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.

 
  
  
  1. $full _name$first _name"$last _name;
  2. $full _name"$first _name $last _name";

The processing of single and double quotation marks in PHP is not the same. The contents of a word in a double-quote string can be parsed and replaced, and the contents of the words in a single quote string are always considered ordinary characters. For example:

 
  
  
  1. $foo = 2;
  2. echo"foo is $foo"//Print Result: Foo is 2
  3. echo' foo is $foo '//Print Result: Foo is $foo
  4. echo"foo is $foon"//Print Result: Foo is 2 (line wrapping)
  5. echo' foo is $foon '

As you can see, even the backslash in the single quote string loses his extended meaning (in addition to adding backslashes \ and adding 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 in more relevant anywhere, script scripts (batch file) using single quotation string processing speed reading will be faster, should be PHP syntax parser for 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 slow.

When referencing a complex combination of variables in a string, some problems may arise, and the following source code will work correctly:

 
  
  
  1. echo"value = $foo";
  2. echo"value = $a [$i]";

The following source code does not get the result we want:

 
  
  
  1. echo"value = $a [$i] [$j]"

To avoid the potential problems in the use of these strings, we often separate complex variables from strings, like this:

 
  
  
  1. echo' value = '$a[$i][$j ];

Another way to do this is to enclose the complex variable in curly braces, which can be correctly identified by the parser:

 
  
  
  1. echo"value = {$a [$i] [$j]}"

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:

 
  
  
  1. $var = 3;
  2. echo"value = {$var}"//Print Result "value = 3"
  3. echo"value = {$var}"

Three, Slash, and SQL statements

Generating HTML source code or SQL query statements is a common and interesting issue when writing PHP programs. Why do you say that? This involves generating another type of source code, and you must carefully consider and follow the writing syntax and rules required by this source code.

Let's look at a case where you want to query the database for users whose names are "O ' Keefe", usually in the form of SQL statements:

 
  
  
  1. last_name = ' O ' Keefe '

Please indicate that the SQL statement (apostrophe) must be escaped with a backslash. PHP specifically gives a number of functions to handle this situation, the function of Addslashes ($STR) is the purpose of the electronic in the string of quotation mark characters into the backslash escape character:

 
  
  
  1. $last _name"O ' Keefe";
  2. $sql"SELECT * from users where last_name = '"addslashes( $last _name "'";

In this case, you will also enclose the single quotation mark (SQL syntax requirement) outside the last_name string, because there is a double quote string, so there is no need to use escape for this pair of single quotes. The following statement is the equivalent of using a single quote string:

 
  
  
  1. $sql' select * from users where last_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 common mistake for many PHP beginners.

Four, double quotes and HTML

Unlike SQL statements, double quotes are often used to express strings in the standard HTML language (many browsers now have strong fault tolerance, allowing the use of single quotes in HTML or even without quotes), for example:

 
  
  
  1. $html'. $url . ' > ' . $link . '' ;
  2. $html"$link";

The HTML language does not support backslash escaping, which is something that we experience when we use the hidden inputs of the list to transfer data. The best way to set the value of hidden inputs is to encode it using the Htmlspecialchars () function. The following statement can normally transmit a data that may contain double quotes:

 
  
  
  1. < input type=hiddenname= var value = " " >

Through the introduction of this article, I hope to help you.


http://www.bkjia.com/PHPjc/445809.html www.bkjia.com true http://www.bkjia.com/PHPjc/445809.html techarticle In a programming language, both single and double quotes are important, as is the case in the PHP language. Compared with ASP, PHP quotation marks better use, the following for you to introduce the single citation ...

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