《PHP實戰:簡單概括PHP的字符串中單引號與雙引號的區別》要點:
本文介紹了PHP實戰:簡單概括PHP的字符串中單引號與雙引號的區別,希望對您有用。如果有疑問,可以聯系我們。
本日有個朋友問起,說下區別,順便復習下.PHP應用
單引號與雙引號的分歧:PHP應用
$hello= 3; echo "hello is $hello"; // 打印結果:hello is 3 echo 'hello is $hello'; // 打印結果: hello is $hello echo "hello is $hello\n"; // 打印結果: hello is 2 (同時換行) echo 'hello is $hello\n'; // 打印結果: hello is $hello\n
PS:
本日看到老外提到了PHP的單引號的問題,其中提到了有趣的東西,摘錄如下:
其中說裝了PHP擴展 Vulcan Logic Disassembler 后,可以看到PHP生成的中間碼,
首先是:PHP應用
echo "This is a string";
會改變為:
PHP應用
ECHO 'This is a string'
而PHP應用
echo 'This is a string';
則釀成PHP應用
ECHO 'This is a string'
,是一樣的
如果是PHP應用
echo "This is a $variable";
則PHP發生的OPCODE為
PHP應用
INIT STRING ~0 2 ADD_STRING ~0 ~0 'This' 3 ADD_STRING ~0 ~0 ' ' 4 ADD_STRING ~0 ~0 'is' 5 ADD_STRING ~0 ~0 ' ' 6 ADD_STRING ~0 ~0 'a' 7 ADD_STRING ~0 ~0 ' ' 8 ADD_VAR ~0 ~0 !0 9 ECHO ~0
而PHP應用
echo "This is a " . $variable;
則會釀成
PHP應用
CONCAT ~0 'This is a ' !0 2 ECHO ~0
可以見到,速度快許多了,用.連接的話PHP應用