標籤:
在php中,結果輸出一共有兩種方式:echo和print,下面將對兩種方式做一個比較。
echo與print的區別:
| |
echo |
print |
| 連續輸出字串 |
能連續輸出多個字串 |
只能輸出一個字串 |
| 傳回值 |
無 |
返回1 |
| 用法 |
echo或echo() |
print或print() |
(1)echo能連續輸出多個字串,print只能輸出一個字串:
執行個體1:
<?php/*echo能連續輸出多個字串,print只能輸出一個字串*/echo "echo輸出一個字串:";echo "hello,world"; //echo輸出一個字串echo "<br/>";echo "echo輸出多個字串:";echo "hello,world","hello,php","hello,python"; //echo輸出多個字串echo "<br/>";print "print輸出一個字串:";print "hello,world"; //print輸出一個字串print "<br/>";/*start-【print連續輸出多個字串】-start*/
print "print輸出多個字串:";print "hello,world","hello,php","hello,python"; //print輸出多個字串,出錯提示:Parse error: syntax error, unexpected ‘,‘ in C:\Users\13842\PhpstormProjects\test\print&echo.php on line 14/*end-【print連續輸出多個字串】-end*/print "<br/>";?>
屏蔽【print連續輸出多個字串】的代碼,結果如下:
如果不屏蔽【print連續輸出多個字串】的代碼,出現錯誤(語法錯誤):
(2)echo無傳回值,print永遠返回1
<?php/*print返回1,echo無傳回值*/$print_value=print "hello,world<br/>"; //結果:hello,worldprint "傳回值為$print_value"; //結果:傳回值為1$echo_value=echo "hello,world<br/>"; //出現語法錯誤?>
(3)輸出方式,帶括弧和不帶括弧沒有什麼區別,這裡不做解釋。
Technorati 標籤: php,echo,print
php學習日誌(3)-echo&print