php中单双引号的区别
” ” 双引号里面的字段会经过编译器解释,然后再当作HTML代码输出。
‘ ‘ 单引号里面的不进行解释,直接输出。
从字面意思上就可以看出,单引号比双引号要快了。
例如:
$abc=’my name is tome’;
echo $abc //结果是:my name is tom
echo ‘$abc’ //结果是
abc
echo “$abc” //结果是:my name is tom
特别在使用MYSQL语句的时候,双引号和单引号的用法让新手不知所措,在这里,举个例子,来进行说明。
假设查询条件中使用的是常量,例如:
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:” ‘ ”
1,2,3部分字符串之间用”.” 来连接
====
一.首先想想PHP里所有的单词(其实应该叫符号)有几类.
1.PHP,mysql两方的关键词与函数.例如echo,print,mysql_connect等等.这些肯定不加引号的.
2.常量.新手可能用得不多,常量的好处是全局性,穿透函数.速度也快些不过新手可以暂时不管常量这玩意儿.
3.变量.前面带"$"号的就是变量.可以为变量设一个"值",例如一串字符,一个数字,逻辑(真/假)值等.也可以表示一组值(数组,对象等)
4.值.通常要给常量与变量设置值.赋值语句$a=\'abc\'中,右边的\'abc\'即为值.
5.函数的参数(在括号里的).可以是常量,变量,值三种.
变量(常量)与值的关系正如下列各种情况.
"颜色"与"红",
"长度"与100,
"日期"与2007年10月25号"
二.什么情况下用PHP引号
其实只有第4项"值"需要用到引号,函数的里也只有值要用引号.并且只有字符串(日期值可以当成字符串)内容需要用到引号.数字(可用可不用),真假(不能用)例外.
例子
三.单引号与双引号的区别
一般情况下两者是通用的.但双引号内部变量会解析,单引号则不解析.
例子
所以如果内部只有纯字符串的时候,用单引号(速度快),内部有别的东西(如变量)的时候,用双号引更好点.
四.字符串内部如果出现PHP引号怎么办--关于转义.
比如我们想输出: 我"是\'天才
这时候就必须用到转义了.转义即把本来有作用的符号转成无意义的字符.
这样就正常了,因为号把它后面的任何字符都转成无意义的符号.在这里来说,PHP解析器根本没把号后面的引号当成引号来看待.
同样的,还可以转义分号,$符号等特殊符号.
五.字符串的连接.
这是个麻烦的问题.一般来说,变量值,直接包含在双引号中就可以了.另外字符串的叠加用"."字符.
在复杂的情况里可以用大括号来包含,PHP便知道这是一个完整的东西,里面的引号不会影响到外面的引号关系.
与html的混合也很简单,最好养成HTML中全部用双引号,PHP中尽量用单引号的习惯.这样方便把大段的HTML代码复制过来,只要头尾加上单引号就是一个正确的字符串了.几百行的HTML代码也不用担心PHP引号错乱.
总结一下PHP引号使用原则
1.字符串的值用引号
2.PHP中尽量用单引号,HTML代码全部用双引号
3.在包含变量的时候,用双引号可以简化操作
4.复杂的情况下用大括号包起来

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

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.

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 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 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.

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

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 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.
