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部分字符串之間用」.」 來鏈接  

相關文章
相關標籤/搜索