Single quotes, double quotes, and escape characters in PHP

Source: Internet
Author: User
single quotes, double quotes, and escape characters in PHP http://blog.csdn.net/river131/article/details/9186203
Category: PHP notes 2013-06-27 11:37 875 people read Comments (0) Favorites report escape characters PHP

PHP single and double quotes can all be decorated with string-type data, if the decorated string contains a variable (example $name); The biggest difference is that double quotes replace the value of the variable, and the single quotation marks it as a string output.

For example:

<?php

$name = "string";

echo "string". ' $name ';

echo "string". "$name";

?>

Results:

string $name

Strings String

Escape characters, as the name suggests, will be used to output the specified syntax "\". But the syntax rules for different systems to relay the role of the semantic characters, such as: windows in the carriage return line break with "\ r" or "\ n", and Linux these two have a big difference: "\ r" cursor back to the beginning, but still in the bank; "\ n" means the next line, and will not return to the beginning.

the escape characters in PHP are:

"\ n" ==> line wrap

"\ r" ==> carriage return

' \ t ' ==> horizontal tab

"\" ==> back Slash

"\$" ==> dollar sign

"\ '" ==> single quote

"\" "==> double quotes

The "\[0-7]{1,3}" regular expression matches a character represented by a octal symbol

The "\x[0-9a-fa-f]{1,2}" regular expression matches a character represented by a hexadecimal symbol

In PHP, you can use the Echo () and print () statements to send data to a Web browser, or you can use them to send HTML code to a Web browser. Technically, Echo () and print () are language constructs, not functions, which use a pair of parentheses to help separate from variables and other parts of PHP. The two statements are essentially indistinguishable, so there is no problem with that one. It's a personal habit. They do not distinguish between size words.

In PHP, it is important to understand the difference between single quotes and double quotes. When you are sending data that involves single and double quotes, use single quotes when printing double quotes, and vice versa, such as:

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

Print "I ' m just Ducky";

Alternatively, you can escape it by placing a backslash in front of the problematic character:

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, putting variables and special characters (see table 1) in double quotes will result in printing the values they represent, not their literal values. Such as:

$var = ' Test ';

Code echo "Var is equal to $var"; Will print out the Var is equal to test, while the code Echo ' var is equal to $var '; Will print out the Var is equal to $var. Using an escaped dollar sign, code echo "\ $var is equal to $var"; Will print out $var is equal to test, and code echo ' \ $var is equal to $var '; will print out \ $var is equal to $var.

Table 1 When these characters are used within double quotes, they have special meanings

Escape character code Meaning of the escape character
\ " Double quotes
\ ' Single quotation mark
\ \ Back slash
\ n Line feed
\ r return character
\ t Tabs
\ $ Dollar sign

As the above example illustrates, double quotes replace its name ($var) with the value of the variable (test), and the value ($) represented by the special character replaces its code (\$). Single quotes always print exactly what you enter, and in addition to escaping single quotes (\ ') and escaped backslash (\), they will be printed as single quotes and a backslash respectively.

Tip:

1, because PHP will try to find the variables that need to be inserted into double quotes, it's theoretically quicker to use single quotes. However, if you need to print the value of a variable, you must use double quotes.

2, because effective HTML often includes many properties that are enclosed in double quotes, it is easiest to use single quotes when printing html using PHP.

Echo ' <table width= "border=" 1 "cellspacing=" 5 "cellpadding=" 2 "align=" center ">";

If you want to use double quotes to print out the above HTML code, you will have to escape all the double quotes in the string, so it's relatively cumbersome.

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.