為什麼很多人都說用單引號會讓 PHP 執行速度比較快?
原因在於 PHP 會解譯「雙引號」字串內的變數,而「單引號」則視為純字串出,PHP 不會再處理單引號內的內容。
如果還是聽不懂在講什麼,可以參考範例:
$var = $value; // ok
$var = "$value"; // ok, but double quotes are not necessary
$var = '$value'; // will not work (single quotes will not allow parsing)
('.' the period adds/connects variables, functions, etc. together.
Oftentimes programmers will leave spaces around the ' . ' to make
things easier to read.)
$var = 'This is the ' . $value . ' of things.'; // ok - preferred
technique
$var = "This is the $value of things."; // ok, but harder to read/debug
$var = 'This is the $value of things.'; // will not parse $value
$var = This is the $value of things.; // error
$var = $array['name']; // ok, generally the preferred technique
$var = $array["name"]; // ok, but why use double quotes if they are not
necessary?
$var = "$array[name]"; // ok, but harder to read/debug - poor coding
style
$var = 'Name: ' . $array['name']; // ok - preferred technique
$var = "Name: $array[name]"; // ok, but harder to read/debug - poor
coding style
$var = "Name: $array["name"]"; // error
$var = "Name: $array['name']"; // error
exampleFunction($value); // ok
exampleFunction("$value"); // ok, but double quotes are not necessary
exampleFunction('$value'); // will not parse $value





2 comments On [PHP] 單引號跟雙引號的不同
嗯,簡單明瞭
小弟最近在學PHP
站長網站裡有好多資料
都好有幫助
以後還請多多指教
聽說新版的 PHP 7 已經沒有差別了。