php print() 函数输出打印字符串

时间:2016-05-18
php print() 函数用于输出一个或多个字符串。print() 函数实际不是一个函数,所以您不必对它使用括号。本文章向码农介绍php print() 函数的基本语法和使用实例。

定义和用法

print() 函数输出一个或多个字符串。

注释:print() 函数实际不是一个函数,所以您不必对它使用括号。

提示:print() 函数比 echo() 稍慢。

语法

print(strings)
参数 描述
strings 必需。发送到输出的一个或多个字符串。

技术细节

返回值: 始终返回 1。
PHP 版本: 4+

实例

例子 1

输出字符串变量($str)的值:

<?php
$str = "I love Shanghai!";
print $str;
?>

在线运行

例子 2

输出字符串变量($str)的值,包含 HTML 标签:

<?php
$str = "I love Shanghai!";
print $str;
print "<br>What a nice day!";
?>

在线运行

例子 3

连接两个字符串变量:

<?php
$str1 = "I love Shanghai!";
$str2="What a nice day!";
print $str1 . " " . $str2;
?> 

在线运行

例子 4

输出数组的值:

<?php
$age=array("Bill"=>"60");
print "Bill Gates is " . $age['Bill'] . " years old.";
?>

在线运行

例子 5

输出文本:

<?php
print "This text
spans multiple
lines.";
?> 

在线运行

例子 6

单引号和双引号的区别。单引号将输出变量名称,而不是值:

<?php
$color = "red";
print "Roses are $color";
print "<br>";
print 'Roses are $color';
?>

在线运行