


Introduction to the difference and usage of single quotes and double quotes in php_PHP tutorial
The article uses a large number of implementations to introduce the usage and difference between single quotes and double quotes. Students who need to know more can refer to this article carefully.
In PHP, usually a string is defined within a pair of quotes
Follow the steps below:
The code is as follows | Copy code | ||||
|
Single quotes: Using single quotes is the most efficient method because PHP does not check built-in variables and escape sequences in single-quoted strings. The only characters that need to be escaped are backslashes and single quotes themselves.
Double quotes:
代码如下 | 复制代码 |
$out = str_replace(array("rn", "r", "n"), '', $out); |
Local documentation:
代码如下 | 复制代码 |
echo
this is a "here document" example. just for test. |
The code is as follows | Copy code |
echo
this is a "here document" example.
just for test.
|
代码如下 | 复制代码 |
'I am a string in single quotes' |
, such as:
The code is as follows | Copy code |
'I am a string in single quotes' "I am a string in double quotes" |
代码如下 | 复制代码 |
"I am not a valid string since I have unmatching quote marks' |
The code is as follows | Copy code |
"I am not a valid string since I have unmatching quote marks' 'Me neither!" |
When defining a string, only one kind of quotation mark is considered as the delimiter, that is, single quotation mark or double quotation mark. So, if a string is represented by a double quote
, then only double quotes are parsed by the parser. This way, you can include any other character within the double-quoted string, even single-quoted
Number. The following quotation mark strings are legal:
The code is as follows | Copy code | ||||||||||||
$s = 'I am a "double quote string" inside a single quote string';
this - extra characters, the parser cannot handle |
代码如下 | 复制代码 |
$file = "c:windowssystem.ini"; |
Finished. To achieve the purpose of including quotation marks, the parser must ignore its original meaning when encountering ordinary quotation marks in the string. We use the quotation mark
Add a backslash in front of it to tell PHP that this quote is part of the string. The correct representation is like this:
The code is as follows | Copy code |
"Why doesn't "that" work?" |
(English possessive case). You have to be careful with these characters:
The code is as follows | Copy code | ||||
|
The code is as follows | Copy code |
$file = "c:windowssystem.ini"; echo $file; // The print result is: c:windowssystem.ini $file = "c:windowssystem.ini"; echo $file; // The print result is: c:windowssystem.ini |
The code is as follows | Copy code |
$first_name = 'Charlie'; $last_name = 'Brown'; $full_name = $first_name . ' ' . $last_name; |
A common use is to create large blocks of HTML string code. The assignment symbol (=) and the connector (.) can be abbreviated and combined into the (.=) symbol
Number, such as:
The code is as follows | Copy code | ||||||||||||||||
|
3. Use variables in strings
代码如下 | 复制代码 |
$foo = 2; |
directly in a double quoted string
String variables, we can find that the processing results of the following two strings are the same.
The code is as follows | Copy code |
$full_name = $first_name . ' ' . $last_name; $full_name = "$first_name $last_name"; |
Single quote strings and double quote strings are processed differently in PHP. The content in double-quoted strings can be interpreted and replaced, while single-quoted
The contents of the number string are always considered ordinary characters. For example:代码如下 | 复制代码 |
echo "value = $foo"; |
The code is as follows | Copy code | ||||
$foo = 2; echo "foo is $foo"; // Print result: foo is 2
|
The code is as follows | Copy code |
echo "value = $foo"; echo "value = $a[$i]"; |
The code is as follows | Copy code |
echo "value = $a[$i][$j]"; //We want to print an element of the two-dimensional array $a. |
To avoid these potential problems in using strings, we usually separate complex variables from strings, like this:
The code is as follows | Copy code | ||||
|
代码如下 | 复制代码 |
echo "value = {$a[$i][$j]}" //打印二维数组$a的某个元素 |
The code is as follows | Copy code |
echo "value = {$a[$i][$j]}" //Print an element of the two-dimensional array $a |
代码如下 | 复制代码 |
$var = 3; |
The code is as follows | Copy code |
$var = 3; echo "value = {$var}"; // print result "value = 3"
echo "value = {$var}"; // print result "value = {3}" |
3. Slashes and SQL statements
Generating HTML code or SQL query statements is often encountered when writing PHP programs and is an interesting thing. Why do you say that?
代码如下 | 复制代码 |
select * from users where last_name = 'O'Keefe' |
Let’s look at an example. If you want to query the user whose name is “O’Keefe” in the database, the usual SQL statement is in the form
代码如下 | 复制代码 |
$last_name = "O'Keefe"; |
The code is as follows | Copy code |
select * from users where last_name = 'O'Keefe' |
代码如下 | 复制代码 |
$sql = 'select * from users where last_name = '' . addslashes($last_name) . '''; |
The code is as follows | Copy code |
$last_name = "O'Keefe"; $sql = "select * from users where last_name = '" . addslashes($last_name) . "'"; |
The code is as follows | Copy code |
$sql = 'select * from users where last_name = '' . addslashes($last_name) . '''; |
Anytime you write a string into a database, you have to make sure that the quotes inside are properly escaped, which is a lot of PHP
Common mistakes made by beginners.
4. Double quotes and HTML
Unlike SQL statements, double quotes are often used to represent strings in standard HTML language (many browsers now have strong fault tolerance capabilities
Yes, it is allowed to use single quotes or even no quotes to represent strings in HTML), for example:
The code is as follows | Copy code | ||||
$html = "$link"; |
HTML language does not support backslash escaping, which will happen when we use hidden inputs of the form to transmit data Got it. The best way to set the value of hidden inputs is to use the htmlspecialchars() function to encode it. The following statements can be
代码如下 | 复制代码 |
|
The code is as follows | Copy code |
|
1. Quotes define strings. To achieve the purpose of including quotation marks, the parser must ignore its original meaning when encountering ordinary quotation marks in the string. We add a backslash in front of the quotation mark to tell PHP: This quotation mark is part of the string and is the correct representation. The method is this: single quote strings can be used anywhere else. The processing speed of using single quote strings in scripts will be faster, because the PHP parser processes single quote strings in a relatively simple way, while the processing of double quotes is due to the internal nature of the string. It also requires parsing, so it's more complex, so the processing speed is slightly slower.
This...double quotes are escaped, single quotes are not escaped
For example: /r/n is a newline, but if you write a file with single quotes, it will not be a newline, but a character. If you write a file with double quotes, it will be a newline.Finally, check out what other websites have to say
” ” Fields enclosed in double quotes will be interpreted by the compiler and then output as HTML code.
‘ ‘ The words in the single quotes are not interpreted and are output directly.
It can be seen from the literal meaning that single quotes are faster than double quotes.
代码如下 | 复制代码 |
$abc=’my name is tome’; echo $abc //结果是:my name is tom echo ‘$abc’ //结果是:$abc echo “$abc” //结果是:my name is tom |
For example:
The code is as follows | Copy code | ||||
$abc='my name is tome'; echo $abc //The result is: my name is tom echo ‘$abc’ //The result is: $abc
|
The code is as follows | Copy code |
select * from abc_table where user_name=’abc’; |
SQL statement can be written as:
The code is as follows
|
Copy code
|
||||||||||||||||||||||||||
Assume that variables are used in the query conditions, for example:
|

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

The article discusses managing file upload sizes in PHP, focusing on the default limit of 2MB and how to increase it by modifying php.ini settings.

The article discusses nullable types in PHP, introduced in PHP 7.1, allowing variables or parameters to be either a specified type or null. It highlights benefits like improved readability, type safety, and explicit intent, and explains how to declar

The article discusses the differences between unset() and unlink() functions in programming, focusing on their purposes and use cases. Unset() removes variables from memory, while unlink() deletes files from the filesystem. Both are crucial for effec


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
