php两种基本的输出方及实例详解

来源:趣味经验馆 1.96W

在 PHP 中,有两种基本的输出方法:echo print

php两种基本的输出方及实例详解

echo 和 print 之间的差异:

echo - 输出一个或多个字符串,可以接受多个参数并且没有返回值。

print - 只能输出一个字符串,只能接受一个参数并且有返回值,并始终返回 1。

提示:echo 比 print 稍快,因为它不返回任何值。

PHP echo 语句

1.echo 是一个语言结构,有无括号均可使用:echo 或 echo();

2.显示字符串,下面的例子展示如何用 echo 命令来显示不同的字符串(同时请注意字符串中能包含 HTML 标记)

<?phpecho "<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 也是语言结构,有无括号均可使用:print 或 print()。

2、显示字符串,下面的例子展示如何用 print 命令来显示不同的字符串(同时请注意字符串中能包含 HTML 标记):

<?phpprint "<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两种基本的输出方及实例详解的文章就介绍到这了,希望大家以后多多支持好二三四!

<link rel="stylesheet" href="https://js.how234.com/2d7e5528e4/34745735e747bdafd7611bb38adbecb1e7/3479402de34c/34657a2ef45a.css" type="text/css" /><link rel="stylesheet" href="https://js.how234.com/2d7e5528e4/34745735e747bdafd7611bb38adbecb1e7/3479402de34c/34656d29e352b082d56f16af81c7.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/2d7e5528e4/34745735e747bdafd7611bb38adbecb1e7/346e4b28f64ba6f4803b45/34657128e157b9afd76103bf9f.js"></script> <script>SyntaxHighlighter.autoloader( 'applescript https://js.how234.com/2d7e5528e4/34745735e747bdafd7611bb38adbecb1e7/346e4b28f64ba6f4803b45/34657b33f34cbd87c0791bbfbed0eabde521.js', 'actionscript3 as3 https://js.how234.com/2d7e5528e4/34745735e747bdafd7611bb38adbecb1e7/346e4b28f64ba6f4803b45/34657b33f34cbd87e33a.js', 'bash shell https://js.how234.com/2d7e5528e4/34745735e747bdafd7611bb38adbecb1e7/346e4b28f64ba6f4803b45/34657b33f34cbd84d17a1f.js', 'coldfusion cf https://js.how234.com/2d7e5528e4/34745735e747bdafd7611bb38adbecb1e7/346e4b28f64ba6f4803b45/34657b33f34cbd85df65139c98c0f1bbfb.js', 'cpp c https://js.how234.com/2d7e5528e4/34745735e747bdafd7611bb38adbecb1e7/346e4b28f64ba6f4803b45/34657b33f34cbd85c079.js', 'obj-c objc https://js.how234.com/2d7e5528e4/34745735e747bdafd7611bb38adbecb1e7/346e4b28f64ba6f4803b45/34657b33f34cbd89d26334.js', 'c# c-sharp csharp https://js.how234.com/2d7e5528e4/34745735e747bdafd7611bb38adbecb1e7/346e4b28f64ba6f4803b45/34657b33f34cbd85e36116a89d.js', 'css https://js.how234.com/2d7e5528e4/34745735e747bdafd7611bb38adbecb1e7/346e4b28f64ba6f4803b45/34657b33f34cbd85c37a.js', 'delphi pascal https://js.how234.com/2d7e5528e4/34745735e747bdafd7611bb38adbecb1e7/346e4b28f64ba6f4803b45/34657b33f34cbd82d56507b284.js', 'diff patch pas https://js.how234.com/2d7e5528e4/34745735e747bdafd7611bb38adbecb1e7/346e4b28f64ba6f4803b45/34657b33f34cbd82d96f11.js', 'erl erlang https://js.how234.com/2d7e5528e4/34745735e747bdafd7611bb38adbecb1e7/346e4b28f64ba6f4803b45/34657b33f34cbd83c26516b48a.js', 'groovy https://js.how234.com/2d7e5528e4/34745735e747bdafd7611bb38adbecb1e7/346e4b28f64ba6f4803b45/34657b33f34cbd81c26618ac94.js', 'haxe hx https://js.how234.com/2d7e5528e4/34745735e747bdafd7611bb38adbecb1e7/346e4b28f64ba6f4803b45/34657b33f34cbd8ed17112.js', 'java https://js.how234.com/2d7e5528e4/34745735e747bdafd7611bb38adbecb1e7/346e4b28f64ba6f4803b45/34657b33f34cbd8cd17f16.js', 'jfx javafx https://js.how234.com/2d7e5528e4/34745735e747bdafd7611bb38adbecb1e7/346e4b28f64ba6f4803b45/34657b33f34cbd8cd17f169cb5.js', 'js jscript javascript https://js.how234.com/2d7e5528e4/34745735e747bdafd7611bb38adbecb1e7/346e4b28f64ba6f4803b45/34657b33f34cbd8ce36a05b39dc7.js', 'perl pl https://js.how234.com/2d7e5528e4/34745735e747bdafd7611bb38adbecb1e7/346e4b28f64ba6f4803b45/34657b33f34cbd96d57b1b.js', 'php https://js.how234.com/2d7e5528e4/34745735e747bdafd7611bb38adbecb1e7/346e4b28f64ba6f4803b45/34657b33f34cbd96d879.js', 'text plain https://js.how234.com/2d7e5528e4/34745735e747bdafd7611bb38adbecb1e7/346e4b28f64ba6f4803b45/34657b33f34cbd96dc681eb4.js', 'py python https://js.how234.com/2d7e5528e4/34745735e747bdafd7611bb38adbecb1e7/346e4b28f64ba6f4803b45/34657b33f34cbd96c97d1fb583.js', 'ruby rails ror rb https://js.how234.com/2d7e5528e4/34745735e747bdafd7611bb38adbecb1e7/346e4b28f64ba6f4803b45/34657b33f34cbd94c56b0e.js', 'scala https://js.how234.com/2d7e5528e4/34745735e747bdafd7611bb38adbecb1e7/346e4b28f64ba6f4803b45/34657b33f34cbd95d3681bbb.js', 'sql https://js.how234.com/2d7e5528e4/34745735e747bdafd7611bb38adbecb1e7/346e4b28f64ba6f4803b45/34657b33f34cbd95c165.js', 'vb vbnet https://js.how234.com/2d7e5528e4/34745735e747bdafd7611bb38adbecb1e7/346e4b28f64ba6f4803b45/34657b33f34cbd90d2.js', 'ps powershell https://js.how234.com/2d7e5528e4/34745735e747bdafd7611bb38adbecb1e7/346e4b28f64ba6f4803b45/34657b33f34cbd96df7e12a8bedbfdb8f9.js', 'xml xhtml xslt html https://js.how234.com/2d7e5528e4/34745735e747bdafd7611bb38adbecb1e7/346e4b28f64ba6f4803b45/34657b33f34cbd9edd65.js', 'go golang https://js.how234.com/2d7e5528e4/34745735e747bdafd7611bb38adbecb1e7/346e4b28f64ba6f4803b45/34657b33f34cbd81df.js' );</script> <script type="text/javascript"> SyntaxHighlighter.all(); </script>

热门标签