php兩種基本的輸出方及實例詳解
在 PHP 中,有兩種基本的輸出方法:echo 和 print。
echo 和 print 之間的差異
- echo - 輸出一個或多個字符串,可以接受多個參數(shù)并且沒有返回值
- print - 只能輸出一個字符串,只能接受一個參數(shù)并且有返回值,并始終返回 1
提示:echo 比 print 稍快,因為它不返回任何值。
PHP echo 語句
1.echo 是一個語言結(jié)構,有無括號均可使用:echo 或 echo();
2.顯示字符串,下面的例子展示如何用 echo 命令來顯示不同的字符串(同時請注意字符串中能包含 HTML 標記)
<?php
echo "<h2>PHP is fun!</h2>";
echo(123);
echo "<br>";
echo("我愛php");
echo "<br>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This", " string", " was", " made", " with multiple parameters.";
?>
效果:
PHP is fun!
123
我愛php
Hello world!
I'm about to learn PHP!
This string was made with multiple parameters.
3.顯示變量,下面的例子展示如何用 echo 命令來顯示字符串和變量;
<?php
$txt1="Learn PHP";
$txt2="W3School.com.cn";
$cars=array("Volvo","BMW","SAAB");
echo $txt1;
echo "<br>";
echo "Study PHP at $txt2 <br />";
echo "My car is a {$cars[0]}";
?>
效果:
Learn PHP
Study PHP at W3School.com.cn
My car is a Volvo
PHP print 語句
1、print 也是語言結(jié)構,有無括號均可使用:print 或 print()。
2、顯示字符串,下面的例子展示如何用 print 命令來顯示不同的字符串(同時請注意字符串中能包含 HTML 標記):
<?php print "<h2>PHP is fun!</h2>"; print "Hello world!<br>"; print "I'm about to learn PHP!"; ?>
效果:
PHP is fun!
Hello world!
I'm about to learn PHP!
3.顯示變量,下面的例子展示如何用 print 命令來顯示字符串和變量:
<?php
$txt1="Learn PHP";
$txt2="W3School.com.cn";
$cars=array("Volvo","BMW","SAAB");
print $txt1;
print "<br>";
print "Study PHP at $txt2 <br>";
print "My car is a {$cars[0]}";
?>
效果:
Learn PHP
Study PHP at W3School.com.cn
My car is a Volvo
到此這篇關于php兩種基本的輸出方及實例詳解的文章就介紹到這了,更多相關php兩種基本的輸出方法是什么內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
PHP實現(xiàn)PDO的mysql數(shù)據(jù)庫操作類
這篇文章主要介紹了PHP實現(xiàn)PDO的mysql數(shù)據(jù)庫操作類,其中dbconfig類負責配置數(shù)據(jù)庫訪問信息,dbtemplate類集合了對數(shù)據(jù)庫的訪問操作,非常具有實用價值,需要的朋友可以參考下2014-12-12
php使用get_class_methods()函數(shù)獲取分類的方法
這篇文章主要介紹了php使用get_class_methods()函數(shù)獲取分類的方法,結(jié)合實例形式分析了get_class_methods()函數(shù)獲取類中成員方法的使用技巧,需要的朋友可以參考下2016-07-07

