首页  >  文章  >  后端开发  >  php print函数怎么用

php print函数怎么用

藏色散人
藏色散人原创
2019-05-31 11:11:583764浏览

php print函数用于输出一个或多个字符串,其语法是print(strings),参数strings必需,指发送到输出的一个或多个字符串。print()函数实际不是一个函数,所以您不必对它使用括号。

php print函数怎么用

php print函数怎么用?

定义和用法

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

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

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

语法

print(strings)

参数 

strings 必需。发送到输出的一个或多个字符串。

返回值: 始终返回 1。

PHP 版本: 4+

例子 1

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

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

输出:

I love Shanghai!

例子 2

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

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

输出:

I love Shanghai!
What a nice day!

例子 3

连接两个字符串变量:

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

输出:

I love Shanghai! What a nice day!

例子 4

输出数组的值:

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

输出:

Bill Gates is 60 years old.

例子 5

输出文本:

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

输出:

This text spans multiple lines.

例子 6

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

<?php
$color = "red";
print "Roses are $color";
print "<br>";
print &#39;Roses are $color&#39;;
?>

输出:

Roses are red
Roses are $color

例子 7

向输出写入文本:

<?php
print "I love Shanghai!";
?>

输出:

I love Shanghai!

以上是php print函数怎么用的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn