查看: 2381|回复: 0

[在线活动] php两种基本的输出方及实例详解

[复制链接]

该用户从未签到

2

主题

2

帖子

0

新手上路

Rank: 1

积分
18
最后登录
2022-3-25
发表于 2022-3-23 18:17:24 | 显示全部楼层 |阅读模式
在本篇文章里小编给大家整理了一篇关于php两种基本的输出方及实例详解内容,有需要的朋友们可以跟着学习参考下。
在 PHP 中,有两种基本的输出方法:
echo
print


echo 和 print 之间的差异

  • echo - 输出一个或多个字符串,可以接受多个参数并且没有返回值
  • print - 只能输出一个字符串,只能接受一个参数并且有返回值,并始终返回 1

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

PHP echo 语句

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

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

  1. <?php
  2. echo "<h2>PHP is fun!</h2>";
  3. echo(123);
  4. echo "<br>";
  5. echo("我爱php");
  6. echo "<br>";
  7. echo "Hello world!<br>";
  8. echo "I'm about to learn PHP!<br>";
  9. echo "This", " string", " was", " made", " with multiple parameters.";
  10. ?>
复制代码

效果:

PHP is fun!
123
我爱php
Hello world!
I'm about to learn PHP!
This string was made with multiple parameters.

3.显示变量,下面的例子展示如何用 echo 命令来显示字符串和变量;

  1. <?php
  2. $txt1="Learn PHP";
  3. $txt2="W3School.com.cn";
  4. $cars=array("Volvo","BMW","SAAB");

  5. echo $txt1;
  6. echo "<br>";
  7. echo "Study PHP at $txt2 <br />";
  8. echo "My car is a {$cars[0]}";
  9. ?>
复制代码

效果:

Learn PHP
Study PHP at W3School.com.cn
My car is a Volvo

PHP print 语句

1、print 也是语言结构,有无括号均可使用:print 或 print()。

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

  1. <?php
  2. print "<h2>PHP is fun!</h2>";
  3. print "Hello world!<br>";
  4. print "I'm about to learn PHP!";
  5. ?>
复制代码

效果:

PHP is fun!
Hello world!
I'm about to learn PHP!

3.显示变量,下面的例子展示如何用 print 命令来显示字符串和变量:

  1. <?php
  2. $txt1="Learn PHP";
  3. $txt2="W3School.com.cn";
  4. $cars=array("Volvo","BMW","SAAB");

  5. print $txt1;
  6. print "<br>";
  7. print "Study PHP at $txt2 <br>";
  8. print "My car is a {$cars[0]}";
  9. ?>
复制代码

效果:

Learn PHP
Study PHP at W3School.com.cn
My car is a Volvo

到此这篇关于php两种基本的输出方及实例详解的文章就介绍到这了。



回复

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条

Archiver|手机版|小黑屋|恩智浦技术社区

GMT+8, 2025-7-19 23:06 , Processed in 0.081365 second(s), 18 queries , MemCache On.

Powered by Discuz! X3.4

Copyright © 2001-2024, Tencent Cloud.

快速回复 返回顶部 返回列表