freeCodeCamp/guide/chinese/php/php-echo-print/index.md

89 lines
1.9 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
title: PHP 5 echo and print Statements
localeTitle: PHP 5回显和打印语句
---
在PHP中有两种基本的输出方式echo和print。
在本教程中我们几乎在每个示例中都使用echo和print。因此本章包含有关这两个输出语句的更多信息。
### PHP回显和打印语句
回声和打印或多或少相同。它们都用于将数据输出到屏幕。
差异很小echo没有返回值而print的返回值为1因此可以在表达式中使用。 echo可以采用多个参数虽然这种用法很少见而print可以采用一个参数。回声比打印快一点。
### PHP echo语句
echo语句可以带括号或不带括号使用echo或echo
#### 显示文字
以下示例显示如何使用echo命令输出文本请注意文本可以包含HTML标记
#### 例
```php
<?php
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?>
```
#### 显示变量
以下示例显示如何使用echo语句输出文本和变量
#### 例
```php
<?php
$txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
$x = 5;
$y = 4;
echo "<h2>" . $txt1 . "</h2>";
echo "Study PHP at " . $txt2 . "<br>";
echo $x + $y;
?>
```
### PHP打印声明
print语句可以带括号或不带括号使用print或print
#### 显示文字
以下示例显示如何使用print命令输出文本请注意文本可以包含HTML标记
#### 例
```php
<?php
print "<h2>PHP is Fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>
```
#### 显示变量
以下示例显示如何使用print语句输出文本和变量
#### 例
```php
<?php
$txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
$x = 5;
$y = 4;
print "<h2>" . $txt1 . "</h2>";
print "Study PHP at " . $txt2 . "<br>";
print $x + $y;
?>
```