Home >Backend Development >PHP Tutorial >8 PHP 5 echo and print statements

8 PHP 5 echo and print statements

WBOY
WBOYOriginal
2016-08-08 09:20:02935browse

In PHP, there are two basic output methods: echo and print.

PHP echo and print statements

Difference between echo and print:

<code>echo - 能够输出一个以上的字符串
print - 只能输出一个字符串,并始终返回 1
</code>

Tip: echo is slightly faster than print because it does not return any value.


PHP echo statement

echo is a language structure, which can be used with or without parentheses: echo or echo().
Displaying strings

The following example shows how to use the echo command to display different strings (please also note that strings can contain HTML tags):

<code><span><span><?php</span><span>echo</span><span>"<h2>PHP is fun!</h2>"</span>;
<span>echo</span><span>"Hello world!<br>"</span>;
<span>echo</span><span>"I'm about to learn PHP!<br>"</span>;
<span>echo</span><span>"This"</span>, <span>" string"</span>, <span>" was"</span>, <span>" made"</span>, <span>" with multiple parameters."</span>;
<span>?></span></span></code>

The following example shows how to use the echo command to display strings and variables:

<code><span><?php</span><span>$txt1</span>=<span>"Learn PHP"</span>;
<span>$txt2</span>=<span>"W3School.com.cn"</span>;
<span>$cars</span>=<span>array</span>(<span>"Volvo"</span>,<span>"BMW"</span>,<span>"SAAB"</span>);

<span>echo</span><span>$txt1</span>;
<span>echo</span><span>"<br>"</span>;
<span>echo</span><span>"Study PHP at $txt2"</span>;<span>// 单引号解析不了变量$</span><span>echo</span><span>"My car is a {$cars[0]}"</span>;
<span>?></span></code>

PHP print statement

print is also a language structure and can be used with or without parentheses: print or print().
Displaying strings

The following example shows how to use the print command to display different strings (please also note that strings can contain HTML tags):

<code><span><span><?php</span><span>print</span><span>"<h2>PHP is fun!</h2>"</span>;
<span>print</span><span>"Hello world!<br>"</span>;
<span>print</span><span>"I'm about to learn PHP!"</span>;
<span>?></span></span></code>

Display variables

The following example shows how to use the print command to display characters String and variable:

<code><span><?php</span><span>$txt1</span>=<span>"Learn PHP"</span>;
<span>$txt2</span>=<span>"W3School.com.cn"</span>;
<span>$cars</span>=<span>array</span>(<span>"Volvo"</span>,<span>"BMW"</span>,<span>"SAAB"</span>);

<span>print</span><span>$txt1</span>;
<span>print</span><span>"<br>"</span>;
<span>print</span><span>"Study PHP at $txt2"</span>;
<span>print</span><span>"My car is a {$cars[0]}"</span>;
<span>?></span></code>

Copyright statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above introduces 8 PHP 5 echo and print statements, including relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn