English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
PHP single quotes and double quotes can both be used to modify data of string type. If the modified string contains variables (e.g., $name), the biggest difference is: double quotes will replace the variable's value, while single quotes will output it as a string.
For example:
<?php $name="string"; echo "string" . '$name'; echo "String" . "$name"; ?>
Result:
String $name
String string
Escape characters, as the name implies, will output the specified syntax using \". However, the effect of escape characters may vary in different systems, for example, the carriage return and line feed in Windows use \r or \n, while in Linux, there is a big difference: \r moves the cursor to the beginning of the line but remains on the same line; \n represents the next line without moving to the beginning of the line.
PHP has the following escape characters:
" \n" ==> newline
"\r"==> carriage return
"\t"==> horizontal tab
"\\"==> backslash
"\$" ==> dollar sign
" \' "==> single quotes
" \" " ==> double quotes
" \[0-7]{1,3Regular expression matches a character represented by an octal symbol
" \x[0-9A-Fa-f]{1,2Regular expression matches a character represented by a hexadecimal symbol
In PHP, you can use 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, and the use of parentheses helps to distinguish them from variables and other parts of PHP. These two statements are essentially the same, so there is no problem using either one. It depends on personal preference. They do not distinguish between uppercase and lowercase.
It is important to understand the difference between single quotes and double quotes in PHP. When the data being sent involves single quotes and double quotes, use single quotes when printing double quotes, and vice versa, as shown:
echo 'She said,"How are you\"63;"'; print "I'm just ducky.";
Or, by placing a backslash before the problematic character, you can escape it:
echo "She said,\"How are you\"63;\" "; print 'I\'m just ducky.';
In PHP, values enclosed in single quotes are treated literally, while values enclosed in double quotes are interpreted. In other words, variables and special characters (see table1Placing them inside double quotes will cause the program to print out their represented values rather than their literal values. For example:
$var = 'test';
The code echo "var is equal to $var";
It will print out 'var is equal to test', while the code echo 'var is equal to $var';
Will print out var is equal to $var. Use an escaped dollar sign, code echo"\$var is equal to $var";
Will print out $var is equal to test, while the code echo '\$var is equal to $var';
Will print out \$var is equal to $var.
Table1 These characters have special meanings when used within double quotes.
Escape character code | Meaning of escape characters |
\ " | Double quote |
\ ' | Single quote |
\ \ | Backslash |
\ n | New line |
\ r | Carriage return |
\ t | Tab |
\ $ | Dollar sign |
As shown in the above example, double quotes will replace the name (test) of the variable with its value, and the code (\$) represented by special characters will replace its code (\$). Single quotes always print the content you input accurately, except for the escaped single quote (\') and the escaped backslash (\\), which will be printed as a single quote and a backslash, respectively.
Tip:
1Because PHP will try to find out which variables need to have their values inserted within double quotes, theoretically, using single quotes is faster.However, if you need to print the value of a variable, you must use double quotes.
2Because 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="200" border="1" cellspacing="5" cellpadding="2" align="center">';
If you want to print the above HTML code with double quotes, you will have to escape all the double quotes in the string, so it is relatively麻烦.
This is the full content of the detailed explanation of single quotes, double quotes, and escape characters in PHP that the editor has shared with everyone. I hope it can be a reference for everyone, and I also hope everyone will support the Shouting Tutorial.