Single quotes, double quotes, and escape characters in php

Source: Internet
Author: User
PHP single quotes and double quotes can modify string data. if the modified string contains a variable (for example, $ name), the biggest difference is that double quotes replace the variable value, the single quotation mark will output it as a string. Example: & lt ;? Php $ name = & quot; string & quot; echo & quot; string... "/> <scripttype =" text

PHP single quotes and double quotes can modify string data. if the modified string contains a variable (for example, $ name), the biggest difference is that double quotes replace the variable value, the single quotation mark will output it as a string.

For example:

$ Name = "string ";

Echo "string". '$ name ';

Echo "string". "$ name ";

?>

Result:

String $ name

String

 

 

Escape characters. as the name suggests, the specified syntax is output. However, the syntax specifies that different system escape characters have different functions. for example, in windows, the carriage return line breaks use "\ r" or "\ n ", linux has a big difference: "\ r", the cursor returns to the beginning of the line, but it is still in the line; "\ n" indicates the next line, will not return to the first line.

Escape characters in PHP include:

"\ N" => line feed

"\ R" => enter

"\ T" => horizontal tab

"\" => Backslash

"\ $" ==> Dollar sign

"\ '" => Single quotes

"\" "=> Double quotation marks

"\ [0-7] {}" regular expression matches the character represented by an octal symbol

"\ X [0-9A-Fa-f] {1, 2}" regular expression matches a character represented by a Hexadecimal Symbol

 

 

 

In PHP, you can use echo () and print () statements to send data to the Web browser, or use them to send HTML code to the Web browser. Technically, echo () and print () are language constructs rather than functions. here we use a pair of parentheses to help distinguish them from variables and other parts of PHP. These two statements have no difference in nature, so using them will not produce any problems. This depends on your habits. They do not differentiate characters in size.

In PHP, it is important to understand the difference between single quotes and double quotes. When the sent data involves single and double quotation marks, use single quotation marks when printing double quotation marks, and vice versa, such:

Echo 'She said, "How are you? "';

Print "I'm just ducky .";

Alternatively, place a backslash before a problematic character to escape it:

Echo "She said, \" How are you? \"";

Print 'I \'m just ducky .';

In PHP, values enclosed in single quotes are processed literally, while values enclosed in double quotes are interpreted. In other words, placing variables and special characters (see table 1) in double quotation marks will result in printing the values they represent, rather than their literal values. For example:

$ Var = 'test ';

The code echo "var is equal to $ var"; prints out var is equal to test, while the code echo 'Var is equal to $ var '; var is equal to $ var. The code echo "\ $ var is equal to $ var" using an escape dollar sign will print $ var is equal to test, the code echo '\ $ var is equal to $ var'; prints \ $ var is equal to $ var.

Table 1 when these characters are used in double quotes, they have special meanings.

 

 

Escape character code meaning of escape characters
\ "Double quotation marks
\ 'Single quotes
\ Backslash
\ N linefeed
\ R carriage return
\ T tab
\ $ Dollar Sign

 

 

As described in the preceding example, double quotation marks replace the variable value (test) with its name ($ var) and represent the value ($) with special characters) the code that replaces it (\ $ ). The single quotes always accurately print the content you entered, except for the escape single quotes (\ ') and escape backslash, they will be printed as a single quotation mark and a backslash respectively.

Tip:

1. since PHP will try to find the variables that need to insert their values into double quotes, it is faster to use single quotes theoretically. However, to print the value of a variable, double quotation marks must be used.

2. because valid HTML often contains many attributes enclosed by double quotation marks, it is easiest to use single quotation marks when HTML is printed using PHP.

Echo'

If you want to print out the preceding HTML code using double quotation marks, you will have to escape all double quotation marks in the string, so it is relatively troublesome.

 

';

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.