Single quotes, double quotes, and escape characters in PHP

Source: Internet
Author: User

PHP single and double quotation marks can modify the string type of data, if the decorated string contains variables (example $name), The biggest difference is: double quotation marks will 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

The escaped character, as the name implies, will use "\" to output the specified syntax. However, the syntax of a different system in the transfer of the role of the characters, for example: The carriage return under Windows "\ r" or "\ n", and Linux is a big difference: "\ r" cursor back to the beginning of the line, but still in the bank; "\ n" means the next line and does not go back to the beginning.

The escape characters in PHP are:

"\ n" ==> line break

"\ r" ==> carriage return

"\ t" ==> horizontal tab

"\ \" ==> backslash

"\$" ==> dollar sign

"\ '" ==> single quotation mark

"\" " ==> double quotes

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

"\x[0-9a-fa-f]{1,2}" regular expression matches the 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, where a pair of parentheses is used to help separate the variables and other parts of PHP. The two statements are inherently no different, so using that one does not create any problems. It's a personal habit. They do not differentiate between size words.

In PHP, it is important to understand what the difference between single and double quotes is. When single and double quotation marks are involved in the data being sent, single quotes are used when printing double quotes, and vice versa, such as:

echo 'She said, ' How is it? ' ';

Print "I ' m just Ducky.";

Or, escape it by placing a backslash in front of the problematic character:

echo "She said,\" How is you?\ "";

print ' I\ ' m just ducky. ';

In PHP, values enclosed in single quotes are processed literally, and 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 the printing of the values they represent, not their literal values. Such as:

$var = ' Test ';

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

Table 1 When you use these characters in double quotation marks, they have a special meaning

 

escape character code
\ "
\ ' single quote
\ \
\ n line break
\ R carriage return
\ t tabs
\ $ dollar sign

As the above example illustrates, double quotation marks replace its name ($var) with the value of the variable (test), and the value represented by the special character ($) instead of its code (\$). Single quotes always print exactly what you enter, except for the escaped single quotation mark (\ ') and the escaped backslash (\ \), which will be printed as a single quote and a backslash, respectively.

Tips:

1. Because PHP will try to find the variables that need to be inserted into double quotes, in theory, use single quotes faster. However, if you need to print the value of a variable, you must use double quotation marks.

2. Because valid HTML often includes many attributes enclosed in double quotes, it is easiest to use single quotes when printing html with PHP.

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

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

Single quotes, double quotes, and escape characters in PHP

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.