轉自:http://blog.sina.com.cn/s/blog_4f18c3ec0100fsv8.html 單引号和雙引号的差別和效率問題,但還是很多朋友了解的不是很清楚,一直以為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 ‘ ” ; |