当前位置:Gxlcms > PHP教程 > PHP单引号和双引号有哪些区别

PHP单引号和双引号有哪些区别

时间:2021-07-01 10:21:17 帮助过:17人阅读

本文介绍下,php程序中单引号与双引号的区别,以及二者之间的效率问题,有需要的朋友,可以参考下。

很多朋友,一直以为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可以分解为如下的三个部分: 1、”select * from table where user_name = ‘ ” //固定SQL语句 2、$user //变量 3、” ‘ ” 1,2,3部分字符串之间用”.” 来连接。

大家测试下看看效率如何呢?!

人气教程排行