search
Homephp教程php手册PHP learning log (3)-echo&print

      在php中,结果输出一共有两种方式:echo和print,下面将对两种方式做一个比较。

echo与print的区别:

  echo print
连续输出字符串 能连续输出多个字符串 只能输出一个字符串
返回值 返回1
用法 echo或echo() print或print()

(1)echo能连续输出多个字符串,print只能输出一个字符串:

实例1:

<span style="color: #000000">php
</span><span style="color: #008000">/*</span><span style="color: #008000">echo能连续输出多个字符串,print只能输出一个字符串</span><span style="color: #008000">*/</span>
<span style="color: #0000ff">echo</span> "echo输出一个字符串:"<span style="color: #000000">;
</span><span style="color: #0000ff">echo</span> "hello,world";  <span style="color: #008000">//</span><span style="color: #008000">echo输出一个字符串</span>
<span style="color: #0000ff">echo</span> "<br>"<span style="color: #000000">;
</span><span style="color: #0000ff">echo</span> "echo输出多个字符串:"<span style="color: #000000">;
</span><span style="color: #0000ff">echo</span> "hello,world","hello,php","hello,python";   <span style="color: #008000">//</span><span style="color: #008000">echo输出多个字符串</span>
<span style="color: #0000ff">echo</span> "<br>"<span style="color: #000000">;

</span><span style="color: #0000ff">print</span> "print输出一个字符串:"<span style="color: #000000">;
</span><span style="color: #0000ff">print</span> "hello,world";  <span style="color: #008000">//</span><span style="color: #008000">print输出一个字符串</span>
<span style="color: #0000ff">print</span> "<br>"<span style="color: #000000">;
</span><span style="color: #008000">/*</span><span style="color: #008000">start-【print连续输出多个字符串】-start</span><span style="color: #008000">*/</span>
<span style="color: #008000"></span><span style="color: #0000ff">print</span> "print输出多个字符串:"<span style="color: #000000">;
</span><span style="color: #0000ff">print</span> "hello,world","hello,php","hello,python";  <span style="color: #008000">//</span><span style="color: #008000">print输出多个字符串,出错提示:Parse error: syntax error, unexpected ',' in C:\Users\13842\PhpstormProjects\test\print&echo.php on line 14</span><span style="color: #008000">
/*</span><span style="color: #008000">end-【print连续输出多个字符串】-end</span><span style="color: #008000">*/</span><span style="color: #0000ff">print</span> "<br>"<span style="color: #000000">;
</span>?>

屏蔽【print连续输出多个字符串】的代码,结果如下:

PHP learning log (3)-echo&print

如果不屏蔽【print连续输出多个字符串】的代码,出现错误(语法错误):

PHP learning log (3)-echo&print

(2)echo无返回值,print永远返回1

<span style="color: #000000">php
</span><span style="color: #008000">/*</span><span style="color: #008000">print返回1,echo无返回值</span><span style="color: #008000">*/</span>
<span style="color: #800080">$print_value</span>=<span style="color: #0000ff">print</span> "hello,world<br>";  <span style="color: #008000">//</span><span style="color: #008000">结果:hello,world</span>
<span style="color: #0000ff">print</span> "返回值为<span style="color: #800080">$print_value</span>";   <span style="color: #008000">//</span><span style="color: #008000">结果:返回值为1</span>

<span style="color: #800080">$echo_value</span>=<span style="color: #0000ff">echo</span> "hello,world<br>";   <span style="color: #008000">//</span><span style="color: #008000">出现语法错误</span>
?>

(3)输出方式,带括号和不带括号没有什么区别,这里不做解释。

 

 

Technorati 标签: php,echo,print
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)