在 PHP 中,有兩種基本的輸出方法:echo 和 print。
PHP echo 和 print 語句
echo 和 print 之間的差異:
<code>echo - 能够输出一个以上的字符串 print - 只能输出一个字符串,并始终返回 1 </code>
提示:echo 比 print 稍快,因為它不會傳回任何值。
PHP echo 語句
echo 是一個語言結構,有無括號皆可使用:echo 或 echo()。
顯示字串
下面的範例展示如何用echo 指令顯示不同的字串(同時請注意字串中能包含HTML 標記):
<code><span><span><?php</span><span>echo</span><span>"<h2>PHP is fun!</h2>"</span>; <span>echo</span><span>"Hello world!<br>"</span>; <span>echo</span><span>"I'm about to learn PHP!<br>"</span>; <span>echo</span><span>"This"</span>, <span>" string"</span>, <span>" was"</span>, <span>" made"</span>, <span>" with multiple parameters."</span>; <span>?></span></span></code>
下面的範例展示如何用echo 指令來顯示字串和變數:
<code><span><?php</span><span>$txt1</span>=<span>"Learn PHP"</span>; <span>$txt2</span>=<span>"W3School.com.cn"</span>; <span>$cars</span>=<span>array</span>(<span>"Volvo"</span>,<span>"BMW"</span>,<span>"SAAB"</span>); <span>echo</span><span>$txt1</span>; <span>echo</span><span>"<br>"</span>; <span>echo</span><span>"Study PHP at $txt2"</span>;<span>// 单引号解析不了变量$</span><span>echo</span><span>"My car is a {$cars[0]}"</span>; <span>?></span></code>
PHP print 語句
print 也是語言結構,有無括號皆可使用:print 或print()。
顯示字串
下面的範例展示如何用print 指令顯示不同的字串(同時請注意字串中能包含HTML 標記):
<code><span><span><?php</span><span>print</span><span>"<h2>PHP is fun!</h2>"</span>; <span>print</span><span>"Hello world!<br>"</span>; <span>print</span><span>"I'm about to learn PHP!"</span>; <span>?></span></span></code>
顯示變數
下面的範例展示如何用print 指令顯示字元字串與變數:
<code><span><?php</span><span>$txt1</span>=<span>"Learn PHP"</span>; <span>$txt2</span>=<span>"W3School.com.cn"</span>; <span>$cars</span>=<span>array</span>(<span>"Volvo"</span>,<span>"BMW"</span>,<span>"SAAB"</span>); <span>print</span><span>$txt1</span>; <span>print</span><span>"<br>"</span>; <span>print</span><span>"Study PHP at $txt2"</span>; <span>print</span><span>"My car is a {$cars[0]}"</span>; <span>?></span></code>
版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。
以上就介紹了8 PHP 5 echo 和 print 語句,包含了方面的內容,希望對PHP教學有興趣的朋友有幫助。