Differences between single and double quotation marks in PHP programming language _ PHP Tutorial

Source: Internet
Author: User
Tags php programming language
Next we will discuss the difference between double quotation marks in the PHP programming language. In programming languages, both single quotes and double quotes play an important role, as in PHP. PHP quotes are easier to use than ASP quotes. the following section introduces the single quotes in programming languages. Single quotesOr Double quotation marksAnd has a very important role in PHPThe same is true for languages. PHP quotes are more useful than ASP quotes. The following describes the differences between single quotes and double quotes.

1. define a string using quotation marks.

To enclose quotation marks, the parser must ignore the original meaning of the regular quotation marks in a string. we add a backslash before the quotation marks to tell PHP: this quotation mark is a part of the string. the correct expression is as follows: a single quotation mark can be used in more related places, script (batch file) using single quotation marks in processing speed reading will be faster. The PHP syntax parser should handle single quotation marks in a simple way, while double quotation marks need to be resolved internally, so it is more complicated, therefore, the processing speed is slightly slow.

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

 
 
  1. 'I am a string in single quotes'
  2. "I am a string in double quotes"

The PHP syntax parser uses pair quotation marks to judge 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 definition is illegal:

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

When defining a string, only one type of quotation mark is considered as a definition character, that is, single quotation marks or double quotation marks. Therefore, 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 characters or even single quotes in the double quotation marks. The following quotation marks are 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 a quotation mark corresponding to the start of the string, it is considered to have reached the end of the string, so:

 
 
  1. "Why doesn't "this" work?"

In fact, PHP syntax parser is divided into three parts:

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

This -- extra characters cannot be processed by the parser

"Work? "-- Common string

The CASE above tries to include double quotation marks in the double quotation mark string, and the parser considers the string to end when the second double quotation mark is encountered. To enclose quotation marks, the parser must ignore the original meaning of the regular quotation marks in a string. we add a backslash before the quotation marks to tell PHP: this quotation mark is a part of the string. the correct expression is as follows:

 
 
  1. "Why doesn't "that" work?"

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

 
 
  1. 'You'd better escape your apostrophes'

We can see that the backslash has its special meaning in the string. 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; // The printed result is: c: windowssystem. ini.
  3. $ File = "c: \ windows \ system. ini ";
  4. Echo $ file; // The printed result is: c: windowssystem. ini.

Another string definition method can eliminate the troubles of special characters and facilitate reference of long texts. This string defines the method as < <符号紧跟一个自定义字符串开头,最后一行以该自定义字符串结束,并且必须顶格。< p>

2. string links

A string can be connected using a string link character (.), for example:

 
 
  1. $first_name = 'Charlie';
  2. $last_name = 'Brown';
  3. $full_name = $first_name . ' ' . $last_name;

The common purpose is to create a large HTML string source code. The value assignment (=) Link character (.) can be abbreviated to the (. =) symbol, for example:

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

3. use variables in strings

This feature allows you to stick a large number of simple strings without using link symbols. PHP allows us to directly include string variables in double quotes. we can find that the processing results of the following two strings are the same.

 
 
  1. $full_name = $first_name . ' ' . $last_name;
  2. $full_name = "$first_name $last_name";

The processing of single and double quotation marks is different in PHP. The content of a word or work in a double quotation mark string can be parsed and replaced, while the content of a word or work in a single quotation mark string is always considered a common character. For example:

 
 
  1. $ Foo = 2;
  2. Echo "foo is $ foo"; // print the result: foo is 2
  3. Echo 'foo is $ Foo'; // print the result: foo is $ foo
  4. Echo "foo is $ foon"; // print the result: foo is 2 (line feed at the same time)
  5. Echo 'foo is $ foon '; // print the result: foo is $ foon

As you can see, in a single quotation mark string, even the backslash also loses its extended meaning (except adding the backslash \ and adding the single quotation mark '). Therefore, you should use double quotation marks when you want to replace variables in strings and escape sequences including n (linefeeds.

Single quotation marks can be used in more places, and the speed of reading with single quotation marks in script (batch files) is faster, the PHP syntax parser should simply process single quotation marks, while double quotation marks must be parsed internally, so they are more complicated, so the processing speed is slightly slower.

When you reference a complex variable combination in a string, some problems may occur. the following source code works properly:

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

However, the following source code cannot get the expected results:

 
 
  1. Echo "value = $ a [$ I] [$ j]"; // we want to print an element of the two-dimensional array $.

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

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

Another way is to enclose complex variables in curly brackets so that the syntax parser can correctly identify them:

 
 
  1. Echo "value = {$ a [$ I] [$ j]}" // print an element of a two-dimensional array $

In this way, new problems have emerged. When we want to reference the curly braces in a string, remember to use the escape character:

 
 
  1. $ Var = 3;
  2. Echo "value = {$ var}"; // print the result "value = 3"
  3. Echo "value = {$ var}"; // print the result "value = {3 }"

III. slashes and SQL statements

Generating HTML source code or SQL query statements is a common and interesting issue when writing PHP programs. In this case, it should involve generating another type of source code. you must carefully consider and follow the compiling syntax and rules required by the source code.

Let's look at this CASE. if you want to query users whose names are "O" Keefe, the SQL statement is usually like this:

 
 
  1. select * from users where last_name = 'O'Keefe'

Please note that SQL statements must be escaped using backslashes in all English spaces. PHP provides some special functions to deal with such a situation. the purpose of the function AddSlashes ($ str) is simply to add a backslash escape character to the quotation mark character in the string:

 
 
  1. $last_name = "O'Keefe";
  2. $sql = "select * from users where last_name = '" . addslashes($last_name) . "'";

In this CASE, you need to enclose the single quotation marks (SQL syntax requirements) outside the last_name string. because the double quotation marks are used, you do not need to use escape for the single quotation marks. The following statement uses the equivalent form of a single quotation mark string:

 
 
  1. $sql = 'select * from users where last_name = '' . addslashes($last_name) . ''';

At any time you want to write strings in the database, you must ensure that the quotation marks correctly use escape characters. this is a common mistake for PHP beginners.

IV. double quotation marks and HTML

Unlike SQL statements, double quotation marks are often used to express strings in standard HTML languages (many browsers now have strong fault-tolerant features that allow single quotation marks or even no quotation marks to express strings in HTML ), for example:

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

The HTML language does not support backslash escape. this is an experience when we use the hidden inputs in a list to transmit data. The best way to set the value of hidden inputs is to use the htmlspecialchars () function for encoding. The following statement transfers data that may contain double quotation marks:

 
 

I hope this article will help you.


Single quotation marks and double quotation marks (") of" comment "play an important role. They are also used in PHP. PHP quotes are easier to use than ASP quotes. The following describes the single quotes...

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.