PHP单引号和双引号区别

    一直觉得PHP中单引号和双引号是互通的,直到有一天,发现单引号和双引号出现错误的时候才去学习研究。因此今天再拿出来谈谈他们的区别,但愿你们不要再为此困惑。数组

” ” 双引号里面的字段会通过编译器解释,而后再看成HTML代码输出。ide

‘ ‘ 单引号里面的不进行解释,直接输出。学习

    从字面意思上就能够看出,单引号比双引号要快了。字符串

例如:
$abc=’my name is tome’;
echo $abc //结果是:my name is tom
echo ‘$abc’ //结果是:$abc
echo “$abc” //结果是:my name is tom编译器

    特别在使用MYSQL语句的时候,双引号和单引号的用法让新手不知所措,在这里,举个例子,来进行说明。it

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

select * from abc_table where user_name=’abc’;table

SQL语句能够写成:class

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部分字符串之间用”.” 来链接  

相关文章
相关标签/搜索