Php 简明教程

PHP - Echo/Print

在 PHP 中, echoprint 语句都用于在浏览器或 PHP 控制台上呈现输出。它们两个都不是函数,而是语言结构。因此,不应在其中任何一个中使用括号。

In PHP, both echo and print statements are used to render the output either on the browser or the PHP console. Both of them are not functions but they are language constructs. Hence, parentheses should not be used with either of them.

The "echo" Statement in PHP

echo 语句与以下 syntax 搭配使用 −

The echo statement is used with following syntax

echo(string ...$expressions): void

echo 语句输出一个或多个表达式,没有额外的换行符或空格。

The echo statement outputs one or more expressions, with no additional newlines or spaces.

Example

以下是 echo 语句在 PHP 中工作方式的一个示例 −

Here is an example of how the echo statement works in PHP −

<?php
   $name = "Rajesh";
   echo "Hello " . $name . " How are you?"
?>

它将生成以下 output

It will produce the following output

Hello Rajesh How are you?

由于双引号字符串在 PHP 中类似于单引号字符串,因此以下语句会产生相同输出。

Since a double quoted string is similar to a single quoted string in PHP, the following statement produces the same output.

echo 'Hello ' . $name . ' How are you?';

Example

双引号字符串输出变量的值。因此,以下语句在打印输出之前插入“$name”变量的值。

A double quoted string outputs the value of the variable. Hence, the following statement inserts the value of "$name" variable before printing the output.

<?php
   $name = "Rajesh";
   echo "Hello $name How are you?";
?>

它将生成以下 output

It will produce the following output

Hello Rajesh How are you?

Example

但是,单引号字符串将按原样输出“$name”。

But, a single-quoted string will output "$name" as it is.

<?php
   $name = "Rajesh";
   echo 'Hello $name How are you?';
?>

它将生成以下 output

It will produce the following output

Hello $name How are you?

传递给 echo 语句的字符串可以逐个作为多个参数传递,也可以连接在一起作为单个参数传递。因此,以下两个语句都是有效的 −

A string passed to an echo statement can either be passed individually as multiple arguments or concatenated together and passed as a single argument. So, both the following statements are valid −

echo 'Hello ', 'how ', 'are ', 'you?', "\n";
echo 'Hello ' . 'how ' . 'are ' . 'you?' . "\n";

Example

请注意,如果不使用换行字符,两个连续 echo 语句的输出将在同一行中呈现。请看以下示例 −

Note that output of the two successive echo statements will be rendered in the same line if the newline character is not used. Take a look at the following example −

<?php
   echo "hello";
   echo "world";
?>

它将生成以下 output

It will produce the following output

helloworld

The "print" Statement in PHP

print 语句类似于 echo,但它输出表达式。

The print statement is similar to echo, but it outputs an expression.

print(string $expression): int

与 echo 一样,print 也是一种语言结构。它的参数是一个表达式,但它不会放在括号中。

Like echo, print is also a language construct. Its argument is an expression but it is not put in parentheses.

主要区别在于 PHP 中的 print 语句只接受一个参数,并且总是返回 1。

The major difference is that the print statement in PHP accepts a single argument only and always returns 1.

Example

看看下面的 example

Take a look at this following example

<?php
   $name = "Rajesh";

   print "Hello " . $name . " How are you?\n";
   print "Hello $name How are you?";
?>

它将生成以下 output

It will produce the following output

Hello Rajesh How are you?
Hello Rajesh How are you?

Output Multiline Strings Using Print/Echo

echo 和 print 语句都可以输出跨越编辑器中多行内容的多行字符串。请看以下示例 −

Both echo and print statements can output multiline strings spanning over more than one lines in the editor. Take a look at the following example −

<?php
   print "
   Multi-line
   string can be output
   by echo as well as
   print statement in PHP
   ";
?>

它将生成以下 output

It will produce the following output

Multi-line
string can be output
by echo as well as
print statement in PHP

如果把 print 替换成 echo ,输出会保持不变。

The output will remain the same if we replace print with echo.