Home php教程 php手册 php字符串中的双引号与单引号区别

php字符串中的双引号与单引号区别

Jun 13, 2016 am 10:54 AM
php and the difference apostrophe pair variable character string quotation marks how of answer

[php]  

 

/* 

字符串在讲变量的时候已经讲了,怎么又要讲? 

答:之前,只是介绍字符串这种变量类型, 

但对于其定义,性质,函数等等,没有深入的说. 

第2: 字符串,在PHP中,是非常非常重要的一个概念. 

可以这么说: 你在开发网站中,想对字符串做某种操作. 

基本上你想到的字符串的处理功能,系统都已有相关函数了. 

因为PHP自诞生之初,就是为WEB开发而来的, 

因为网站开发中,字符串处理 ,是PHP的一大亮点. 

第3: 字符串在开发和面试时,也是最频繁用到的. 

PHP中,1是字符串,2是数组函数,都非常多,非常好用. 

*/  

/* 

什么是字符串? 

答:字符串,就是一串字符 

羊肉串就是一串羊肉. 

*/  

/* 

1:如何声明一个字符串变量 

答: 

双引号 

单引号 

定界符 

*/  

$str1 = "hello";  

echo $str1,'
';  

// $str2 = "hello,"jack" "; // 这个字符串,是一个新闻标题,标题里很容易又出现双引号.  

/** 

思考: 

用双引号,来声明字符串的时候, 

内部不能再出现双引号, 

因为,双引号被当成字符串的"边界"来对待, 

因此,内部出现双引号,就引起了解释上的歧义. 

这时,可以用转义字符, 

即,用\" 转义 来表示 " 

还有没有其他的转义字符呢? 

想: \", 被用来当成 " 理解了. 

那我要是确实想表示 反斜线\, 又怎么办呢?, 

答: 用\\, 表示 反斜线\ 

还有没有其他的转义字符呢? 

答:有 

\n, \r, \$,等 

\n 转义成 换行符 

\r 转义成 回车符 

**/  

$str2 = "hello ,\"jack\",
";  

echo $str2;  

$str3 = "hello \\";  

echo $str3,'
';  

$str4 = "hello\n\r\n\r\n\rworld";  

echo $str4,'
';  

$str5 = "hello $str3";  

echo $str5,'
';  // hello hello \, 即把$str3当变量给解析出来  

$str6 = "world \$str3"; //此处 \$被转成了普通字符串$,而不再理解为变量标志.  

echo $str6;  

?>  

// 再看单引号  

$str1 = 'hello';  

echo $str1,'
';  

// $str2 = 'hello 'jack''; // 某字符串是新闻标题,标题又出现单引号,  

// 单引号里又出现的单引号怎么办呢?  

// '被用来做字符串的边界了,  

// 因此,串内想出现',只好用转义来表示  

$str2 = 'hello \'jack\'';  

echo $str2,'
';  

// \被用来转义单引号用了,那么如果我确实想表示\,怎么办呢.  

// \\ 来 表示 \  

$str3 = 'hello \\';  

echo $str3;  

?>  

// 常见的面试题  

// 1:单双引号定义字符串有何区别?  

$age = 29;  

$str1 = 'hello \n\r \t $age';  

$str2 = "hello \n\r \t $age";  

echo $str1,'
',$str2;  

/* 

转义上的区别: 

单引号,系统不做复杂的转义, 

只转义\',\\, 其他的---一律原样输出. 

双引号,则转义的比较多,\",\\,\r,\n,\t,\$等. 

对于变量解释的区别: 

单引号,不对字符串的$作变量名解析, 

而双引号,会试着$理解成变量名,来解析. 

*/  

// 2:单双引号定义字符串,哪个速度更快一些?  

// 答:单引号解析的更快,因为不需要进行过多的转义和变量解析  

// 问:什么时间选用单/双引号呢?  

/*  www.2cto.com

答:大段文本,比如新闻标题,文本内容,自我介绍等这样大段话, 

用',解析快. 

但有时候,确实需要在一段字符串夹杂变量, 

比如拼凑sql语句时 

$id = 5; 

$sql = "select * from user where id = $id"; 

// 如果此时用单引,则语句成为了 ..id= $id,sql就出错了. 

// 此时,用双引合适. 解析$id,语句解析成 ..id = 5; 

*/  

?>  

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
The Continued Use of PHP: Reasons for Its Endurance The Continued Use of PHP: Reasons for Its Endurance Apr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

What currency does Ripple (XRP currency) belong to? Detailed tutorial for beginners What currency does Ripple (XRP currency) belong to? Detailed tutorial for beginners Apr 28, 2025 pm 07:57 PM

Created by Ripple, Ripple is used for cross-border payments, which are fast and low-cost and suitable for small transaction payments. After registering a wallet and exchange, purchase and storage can be made.

What happens if session_start() is called multiple times? What happens if session_start() is called multiple times? Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

In back-end development, how to distinguish the responsibilities of the service layer and the dao layer? In back-end development, how to distinguish the responsibilities of the service layer and the dao layer? Apr 19, 2025 pm 01:51 PM

Discussing the hierarchical architecture in back-end development. In back-end development, hierarchical architecture is a common design pattern, usually including controller, service and dao three layers...

Composer: Aiding PHP Development Through AI Composer: Aiding PHP Development Through AI Apr 29, 2025 am 12:27 AM

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

Using Laravel: Streamlining Web Development with PHP Using Laravel: Streamlining Web Development with PHP Apr 19, 2025 am 12:18 AM

Laravel optimizes the web development process including: 1. Use the routing system to manage the URL structure; 2. Use the Blade template engine to simplify view development; 3. Handle time-consuming tasks through queues; 4. Use EloquentORM to simplify database operations; 5. Follow best practices to improve code quality and maintainability.

What is the significance of the session_start() function? What is the significance of the session_start() function? May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

See all articles