Home php教程 php手册 PHP中单引号与双引号的区别分析

PHP中单引号与双引号的区别分析

Jun 06, 2016 pm 08:19 PM
php the difference apostrophe Double quotes

在PHP中,我们可以使用单引号或者双引号来表示字符串。不过我们作为开发者,应该了解其中的区别。单引号与双引号对于定义字符一个是可以解析变量一个是会把变量

①转义的字符不同

单引号和双引号中都可以使用转义字符(\),但只能转义在单引号中引起来的单引号和转义转义符本身。如果用双引号(“”)括起字符串,PHP懂得更多特殊字符串的转义序列。

'; $str2 = "\",\\,a\r\n\tb\v\$\'"; echo $str2,'
'; ?>

②对变量的解析不同
单引号字符串中出现的变量不会被变量值替代。即PHP不会解析单引号中的变量,而是将变量名原样输出。双引号字符串最重要的一点是其中的变量名会被变量值替代,即可以解析双引号中包含的变量。

'; // I am $age years old echo $str2,'
'; // I am 20 years old; ?>

③解析速度不同

单引号不需要考虑变量的解析,速度比双引号快.推荐用单引号.有的时候双引号也比较好用,比如在拼凑sql 语句

反斜杠

//使用单引号 echo ' this \n is \r the blog \t of \\ zhoumanhe \\'; //上面使用单引号输出的值是 this \n is \r the blog \t of \ zhoumanhe \ echo ' '; echo " "; //使用双引号 echo "this \n is \r the blog \t of \\ zhoumanhe \\"; //上面使用双引号输出的值是 this is the blog of \ zhoumanhe \

使用sql

 假设查询条件中使用的是常量,例如:

select * from abc_table where user_name='abc';

SQL语句可以写成:

SQLstr = “select * from abc_table where user _name= ‘abc'” ;

假设查询条件中使用的是变量,,例如:

$user_name = $_REQUEST['user_name']; //字符串变量


$user=array (”name”=> $_REQUEST['user_name‘,"age"=>$_REQUEST['age'];//数组变量

SQL语句就可以写成:

SQLstr = “select * from abc_table where user_name = ‘ ” . $user_name . ” ‘ “; SQLstr = “select * from abc_table where user_name = ‘ ” . $user["name"] . ” ‘ “;

对比一下:

SQLstr=”select * from abc_table where user_name = ‘ abc ‘ ” ; SQLstr=”select * from abc_table where user_name =' ” . $user _name . ” ‘ “; SQLstr=”select * from abc_table where user_name =' ” . $user["name"] . ” ‘ “;

SQLstr可以分解为以下3个部分:

1:”select * from table where user_name = ‘ ” //固定SQL语句 2:$user //变量 3:” ‘ ”

附:大家也看到了 echo '
'; html中的标签在单引号和双引号中都有效。

总结一下PHP引号使用原则

1.字符串的值用引号

2.PHP中尽量用单引号,HTML代码全部用双引号

3.在包含变量的时候,用双引号可以简化操作

4.复杂的情况下用大括号包起来

PHP引号还有一个用处就是,有的时候需要用php生成文本文件,换行符n需要用双引号才能好使,单引号则会直接把n当成字符输出。

使用总结:在字符串里面不需要加入 变量 或者 单引号(')和反斜杠(\) 时,尽量用单引号引字符串,因为省去了双引号检查处理转义和解析变量上面的时间。能用单引号尽量用单引号。

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)

How can you prevent a class from being extended or a method from being overridden in PHP? (final keyword) How can you prevent a class from being extended or a method from being overridden in PHP? (final keyword) Apr 08, 2025 am 12:03 AM

In PHP, the final keyword is used to prevent classes from being inherited and methods being overwritten. 1) When marking the class as final, the class cannot be inherited. 2) When marking the method as final, the method cannot be rewritten by the subclass. Using final keywords ensures the stability and security of your code.

The Future of PHP: Adaptations and Innovations The Future of PHP: Adaptations and Innovations Apr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

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's Current Status: A Look at Web Development Trends PHP's Current Status: A Look at Web Development Trends Apr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

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: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

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.

See all articles