Home php教程 php手册 详细介绍在PHP中单引号和双引号的区别

详细介绍在PHP中单引号和双引号的区别

Jun 13, 2016 am 10:14 AM
php introduce about the difference and exist quotation marks article detailed

文章详细的介绍了关于详细介绍在PHP中单引号和双引号的区别,有需要了解的同学可参考一下。

1、定义字符串   
在PHP中,字符串的定义可以使用单引号,也可以使用双引号。但是必须使用同一种单或双引号来定义字符串,如:‘Hello"和“Hello'为非法的字符串定义。   
定义字符串时,只有一种引号被视为定义符,即单引号或双引号。于是,如果一个字符串由双引号开始,那么只有双引号被分析器解析。这样,你就可以在双引号串中包含任何其他字符,甚至单引号。下面的引号串都是合法的:

Php代码

$s = "I am a 'single quote string' inside a double quote string";

$s = 'I am a "double quote string" inside a single quote string';

$s = "I am a 'single quote string' inside a double quote string";

$s = 'I am a "double quote string" inside a single quote string';   

而串 "Why doesn't "this" work?" 则会被分为三段。如果在这个串中想要表示出双引号,则可以使用转义符""(反斜线),变成 "Why doesn't "this" work?" 即可。

2、字符串变量中的单、双引号   

PHP允许我们在双引号串中直接包含字串变量,我们可以发现下面的两个字串的处理结果是相同的。

$full_name = $first_name . ' ' . $last_name;

$full_name = "$first_name $last_name";   

单引号串和双引号串在PHP中的处理是不相同的。双引号串中的内容可以被解释而且替换,而单引号串中的内容总被认为是普通字符。例如:

Php代码

$foo = 2;

echo "foo is $foo"; // 打印结果: foo is 2

echo 'foo is $foo'; // 打印结果: foo is $foo

echo "foo is $foon"; // 打印结果: foo is 2 (同时换行)

echo 'foo is $foon'; // 打印结果: foo is $foon

$foo = 2;

echo "foo is $foo"; // 打印结果: foo is 2

echo 'foo is $foo'; // 打印结果: foo is $foo

echo "foo is $foon"; // 打印结果: foo is 2 (同时换行)

echo 'foo is $foon'; // 打印结果: foo is $foon   

正如你所看到的,在单引号串中甚至反斜杠也失去了他的扩展含义(除了插入反斜杠\和插入单引号')。所以,当你想在字串中进行变量代换和包 含n(换行符)等转义序列时,你应该使用双引号。单引号串可以用在其他任何地方,脚本中使用单引号串处理速度会更快些,因为PHP语法分析器对单引号串 的处理方式比较单纯,而双引号的处理由于串内部也需要解析,因此更复杂些,所以处理速度略慢。   

在字符串中引用复杂的变量组合时,可能会产生一些问题,下面的代码会正常工作:

Php代码

echo "value = $foo";

echo "value = $a[$i]";

echo "value = $foo";

echo "value = $a[$i]";   

而下面的代码却不能得到我们希望的结果:

echo "value = $a[$i][$j]"; //我们希望打印二维数组$a的某个元素。   

为避免这些字串使用中的潜在问题,我们通常把复杂的变量从字串中分离开来,就像这样:echo 'value = ' . $a[$i][$j];//字符串的连接用点(.)   

还有一种办法是将复杂变量用花括号括起来,语法分析器就能正确辨认了:

echo "value = {$a[$i][$j]}" //打印二维数组$a的某个元素   

这样,又出现新问题了。当我们想在字串中引用花括号字符本身时,就要记得使用转义符了:

Php代码

$var = 3;

echo "value = {$var}"; // 打印结果 "value = 3"

echo "value = {$var}"; // 打印结果 "value = {3}"

$var = 3;

echo "value = {$var}"; // 打印结果 "value = 3"

echo "value = {$var}"; // 打印结果 "value = {3}"

3、在SQL语句中   

这是会经常遇到的问题,在插入数据库的SQL语句是采用单引号来定义字符串,如果要将一个含有单引号的字符串插入数据库,这个SQL语句就会出错。

如:$sql="insert into userinfo (username,password) Values('O'Kefee','123456')"   

此时,处理的方法之一是在SQL语句中加入转义符反斜线,

即:……Values('O'Kefee',……   

当然也可以使用函数 addslashes(),该函数的功能就是加入转义符,

即:$s = addslashes("O'Kefee") ……Values('".$s."',……   

还有一种方法是设置php.ini中的magic-quotes选项,打开该选项,则通过表单提交的信息中如果有单引号是,将会自动加上如转义符。因此不用使用其他函数了。

补充: 这就要从双引号和单引号的作用讲起: 双引号里面的字段会经过编译器解释然后再当作HTML代码输出,但是单引号里面的不需要解释,直接输出。

例如:

$abc='I love u';

echo $abc //结果是:I love u

echo '$abc' //结果是:$abc

echo "$abc" //结果是:I love u

所以在对数据库里面的SQL语句赋值的时候也要用在双引号里面SQL="select a,b,c from ..." 但是SQL语句中会有单引号把字段名引出来

例如:select * from table where user='abc';

这里的SQL语句可以直接写成SQL="select * from table where user='abc'"

但是如果象下面:

$user='abc';

SQL1="select * from table where user=' ".$user." ' ";对比一下

SQL2="select * from table where user=' abc ' "

我把单引号和双引号之间多加了点空格,希望你能看的清楚一点。

也就是把'abc' 替换为 '".$user."'都是在一个单引号里面的。只是把整个SQL字符串分割了。 SQL1可以分解为以下3个部分

1:"select * from table where user=' "

2:$user

3:" ' "

字符串之间用 . 来连接

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1657
14
PHP Tutorial
1257
29
C# Tutorial
1230
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

See all articles