Home > Backend Development > PHP Tutorial > 【转】PHP:echo、print、print_r、sprintf、var_dump其间的区别

【转】PHP:echo、print、print_r、sprintf、var_dump其间的区别

WBOY
Release: 2016-06-13 10:40:23
Original
870 people have browsed it

【转】PHP:echo、print、print_r、sprintf、var_dump之间的区别

- echo
是命令,不能返回值。echo后面可以跟很多个参数,之间用分号隔开,如:

<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$myvar1</span><span style="color: #339933;">;</span><span style="color: #b1b100;">echo</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #000088;">$myvar</span><span style="color: #339933;">,</span><span style="color: #0000ff;">"<b>bold</b>"</span><span style="color: #339933;">;</span>
Copy after login

- print
是函数,可以返回一个值,只能有一个参数。

- printf
函数,把文字格式化以后输出,如:

<span style="color: #000088;">$name</span><span style="color: #339933;">=</span><span style="color: #0000ff;">"hunte"</span><span style="color: #339933;">;</span><span style="color: #000088;">$age</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">25</span><span style="color: #339933;">;</span><span style="color: #990000;">printf</span><span style="color: #009900;">(</span><span style="color: #0000ff;">"my name is <span style="color: #009933; font-weight: bold;">%s</span>, age <span style="color: #009933; font-weight: bold;">%d</span>"</span><span style="color: #339933;">,</span> <span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$age</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
Copy after login

- sprintf
跟printf相似,但不打印,而是返回格式化后的文字,其他的与printf一样。

- print_r
print_r通常用于打印变量的相关信息,通常在调试中使用。

<span style="color: #990000;">print_r</span><span style="color: #009900;">(</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>           <span style="color: #666666;">//输出1</span><span style="color: #990000;">print_r</span><span style="color: #009900;">(</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>          <span style="color: #666666;">//没有输出</span><span style="color: #990000;">print_r</span><span style="color: #009900;">(</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>          <span style="color: #666666;">//没有输出</span>
Copy after login

- var_dump
var_dump此函数显示关于一个或多个表达式的结构信息,包括表达式的类型与值。

<span style="color: #990000;">var_dump</span><span style="color: #009900;">(</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #666666;">//输出 bool(true)</span><span style="color: #990000;">var_dump</span><span style="color: #009900;">(</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>   <span style="color: #666666;">// bool(false)</span><span style="color: #990000;">var_dump</span><span style="color: #009900;">(</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>   <span style="color: #666666;">// bool(null)</span>
Copy after login

var_dump()和print_r()的区别:
var_dump返回表达式的类型与值而print_r仅返回结果,相比调试代码使用var_dump更便于阅读。

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template