初冬微寒 發表於 2020-4-25 10:30:00

php中六种输出方式的区别

<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">php输出&nbsp;<span style="background-color: rgba(136, 136, 136, 1)">echo</span>&nbsp;、&nbsp;<span style="background-color: rgba(136, 136, 136, 1)">print&nbsp;</span>、&nbsp;<span style="background-color: rgba(136, 136, 136, 1)">print_r&nbsp;</span>、&nbsp;<span style="background-color: rgba(136, 136, 136, 1)">printf&nbsp;</span>、&nbsp;<span style="background-color: rgba(136, 136, 136, 1)">sprintf</span>&nbsp;、&nbsp;<span style="background-color: rgba(136, 136, 136, 1)">var_dump</span>&nbsp;的区别比较。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px"><strong>1、echo</strong></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">echo() 实际上不是一个函数,是php语句,因此您无需对其使用括号。不过,如果您希望向 echo() 传递一个以上的参数,那么使用括号会发生解析错误。而且echo是返回void的,并不返回值,所以不能使用它来赋值。</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> &lt;?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span>
<span style="color: rgba(0, 128, 128, 1)"> 3</span> <span style="color: rgba(128, 0, 128, 1)">$a</span> = <span style="color: rgba(0, 0, 255, 1)">echo</span>("words"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 错误!不能用来赋值</span>
<span style="color: rgba(0, 128, 128, 1)"> 4</span>
<span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> "words"; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 55nav</span>
<span style="color: rgba(0, 128, 128, 1)"> 6</span>
<span style="color: rgba(0, 128, 128, 1)"> 7</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> ("words"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 55nav</span>
<span style="color: rgba(0, 128, 128, 1)"> 8</span>
<span style="color: rgba(0, 128, 128, 1)"> 9</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> ("apple","bear"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">发生错误,有括号不能传递多个参数</span>
<span style="color: rgba(0, 128, 128, 1)">10</span>
<span style="color: rgba(0, 128, 128, 1)">11</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> "alice","bill","cart", "daring";<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 不用括号的时候可以用逗号隔开多个值, 会输出 alicebillcartdaring不管是否换行,最终显示都是为一行</span>
<span style="color: rgba(0, 128, 128, 1)">12</span>
<span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(128, 0, 128, 1)">$fistname</span>="alice"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">14</span>
<span style="color: rgba(0, 128, 128, 1)">15</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> "<span style="color: rgba(128, 0, 128, 1)">$fistname</span> com"; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果 $firstname = "alice", 则会输出 alice com.</span>
<span style="color: rgba(0, 128, 128, 1)">16</span>
<span style="color: rgba(0, 128, 128, 1)">17</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> '$firstname com'; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 由于使用单引号,所以不会输出$firstname的值,而是输出 $firstname com</span>
<span style="color: rgba(0, 128, 128, 1)">18</span>
<span style="color: rgba(0, 128, 128, 1)">19</span> ?&gt;</pre>
</div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px"><strong>2、print</strong></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">print() 和 echo() 用法一样,但是echo速度会比print快一点点。实际上它也不是一个函数,因此您无需对其使用括号。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">不过,如果您希望向print() 传递一个以上的参数,那么使用括号会发生解析错误。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">注意print总是返回1的,这个和echo不一样,</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">也就是可以使用print来赋值,不过没有实际意义。</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> &lt;?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 128, 128, 1)">2</span>
<span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(128, 0, 128, 1)">$a</span> = <span style="color: rgba(0, 0, 255, 1)">print</span>("alice"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 这个是允许的</span>
<span style="color: rgba(0, 128, 128, 1)">4</span>
<span style="color: rgba(0, 128, 128, 1)">5</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> <span style="color: rgba(128, 0, 128, 1)">$a</span>; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> $a的值是1</span></pre>
</div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px"><strong>3、print_r 函数</strong></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">print_r函数打印关于变量的易于理解的信息。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">语法:</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">mixed print_r ( mixed $expression [, bool return ] )</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">如果变量是string , integer or float , 将会直接输出其值,如果变量是一个数组,则会输出一个格式化后的数组,便于阅读,也就是有key和value对应的那种格式。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">对于object对象类同。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">print_r有两个参数,第一个是变量,第二个可设为true,如果设为true,则会返回字符串,否则返回布尔值TRUE</span>。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> &lt;?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span>
<span style="color: rgba(0, 128, 128, 1)"> 3</span> <span style="color: rgba(128, 0, 128, 1)">$a</span>="alice"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span>
<span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(128, 0, 128, 1)">$c</span> = <span style="color: rgba(0, 128, 128, 1)">print_r</span>(<span style="color: rgba(128, 0, 128, 1)">$a</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span>
<span style="color: rgba(0, 128, 128, 1)"> 7</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> <span style="color: rgba(128, 0, 128, 1)">$c</span>;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> $c的值是TRUE</span>
<span style="color: rgba(0, 128, 128, 1)"> 8</span>
<span style="color: rgba(0, 128, 128, 1)"> 9</span> <span style="color: rgba(128, 0, 128, 1)">$c</span> = <span style="color: rgba(0, 128, 128, 1)">print_r</span>(<span style="color: rgba(128, 0, 128, 1)">$a</span>,<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">10</span>
<span style="color: rgba(0, 128, 128, 1)">11</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> <span style="color: rgba(128, 0, 128, 1)">$c</span>; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> $c的值是字符串alice</span>
<span style="color: rgba(0, 128, 128, 1)">12</span>
<span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(128, 0, 128, 1)">$d</span> = ;
</span><span style="color: rgba(0, 128, 128, 1)">14</span>
<span style="color: rgba(0, 128, 128, 1)">15</span> <span style="color: rgba(0, 128, 128, 1)">print_r</span>(<span style="color: rgba(128, 0, 128, 1)">$d</span>); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Array ( =&gt; 1 =&gt; 2 =&gt; 3 =&gt; 4 )</span></pre>
</div>
<p><span style="font-size: 16px; font-family: &quot;Microsoft YaHei&quot;"><strong>4、printf函数</strong></span></p>
<p><span style="font-size: 16px; font-family: &quot;Microsoft YaHei&quot;">printf() 函数<strong>返回一个格式化后的字符串</strong>。</span></p>
<p><span style="font-size: 16px; font-family: &quot;Microsoft YaHei&quot;">语法:</span></p>
<p><span style="font-size: 16px; font-family: &quot;Microsoft YaHei&quot;">printf(format,arg1,arg2,arg++)</span></p>
<p><span style="font-size: 16px; font-family: &quot;Microsoft YaHei&quot;">参数 format 是转换的格式,以百分比符号 (“%”) 开始到转换字符结束。</span></p>
<p><span style="font-size: 16px; font-family: &quot;Microsoft YaHei&quot;">下面是可能的 format 值:</span><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">*&nbsp;%%&nbsp;–&nbsp;返回百分比符号</span></p>
<pre class="brush:php;toolbar:false"><em id="__mceDel"><br><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">*&nbsp;%b&nbsp;–&nbsp;二进制数</span><br><br><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">*&nbsp;%c&nbsp;–&nbsp;依照&nbsp;ASCII&nbsp;值的字符</span><br><br><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">*&nbsp;%d&nbsp;–&nbsp;带符号十进制数</span><br><br><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">*&nbsp;%e&nbsp;–&nbsp;可续计数法(比如&nbsp;1.5e+3)</span><br><br><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">*&nbsp;%u&nbsp;–&nbsp;无符号十进制数</span><br><br><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">*&nbsp;%f&nbsp;–&nbsp;浮点数(local&nbsp;settings&nbsp;aware)</span><br><br><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">*&nbsp;%F&nbsp;–&nbsp;浮点数(not&nbsp;local&nbsp;settings&nbsp;aware)</span><br><br><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">*&nbsp;%o&nbsp;–&nbsp;八进制数</span><br><br><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">*&nbsp;%s&nbsp;–&nbsp;字符串</span><br><br><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">*&nbsp;%x&nbsp;–&nbsp;十六进制数(小写字母)</span><br><br><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">*&nbsp;%X&nbsp;–&nbsp;十六进制数(大写字母)<br></span><br></em><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">arg1, arg2, arg++ 等参数将插入到主字符串中的百分号 (%) 符号处。该函数是逐步执行的,在第一个 % 符号中,插入 arg1,在第二个 % 符号处,插入 arg2,依此类推。</span></pre>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">如果 % 符号多于 arg 参数,则您必须使用占位符。占位符被插入 % 符号之后,由数字和 “\$” 组成。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">可使用数字指定显示的参数,详情请看例子。</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> &lt;?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 128, 128, 1)">2</span>
<span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(0, 128, 128, 1)">printf</span>("My name is %s %s。","alice", "com"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> My name is alice com。</span>
<span style="color: rgba(0, 128, 128, 1)">4</span>
<span style="color: rgba(0, 128, 128, 1)">5</span> <span style="color: rgba(0, 128, 128, 1)">printf</span>("My name is %1\$s %1\$s","alice", "com"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 在s前添加1\$或2\$.....表示后面的参数显示的位置,此行输出 My name is alice alice因为只显示第一个参数两次。</span>
<span style="color: rgba(0, 128, 128, 1)">6</span>
<span style="color: rgba(0, 128, 128, 1)">7</span> <span style="color: rgba(0, 128, 128, 1)">printf</span>("My name is %2\$s %1\$s","alice", "com"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> My name is com alice</span>
<span style="color: rgba(0, 128, 128, 1)">8</span>
<span style="color: rgba(0, 128, 128, 1)">9</span> ?&gt;</pre>
</div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px"><strong>5、sprintf函数</strong></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">此函数使用方法和printf一样,<strong>唯一不同的就是该函数把格式化的字符串写入一个变量中,而不是输出来。</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> &lt;?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 128, 128, 1)">2</span>
<span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(0, 128, 128, 1)">sprintf</span>("My name is %1\$s %1\$s","alice", "com");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">你会发现没有任何东西输出的。</span>
<span style="color: rgba(0, 128, 128, 1)">4</span>
<span style="color: rgba(0, 128, 128, 1)">5</span> <span style="color: rgba(128, 0, 128, 1)">$out</span> = <span style="color: rgba(0, 128, 128, 1)">sprintf</span>("My name is %1\$s %2\$s","alice", "com"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">6</span>
<span style="color: rgba(0, 128, 128, 1)">7</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> <span style="color: rgba(128, 0, 128, 1)">$out</span>;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">输出 My name is alice com</span></pre>
</div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px"><strong>6、var_dump函数</strong></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">功能: 输出变量的内容、类型或字符串的内容、类型、长度。常用来调试。</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 128, 128, 1)">var_dump</span>('hello'); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> string(5) "hello"</span></pre>
</div>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/chenyuphp/p/12771824.html
頁: [1]
查看完整版本: php中六种输出方式的区别