The difference between echo, print and print_r function_PHP tutorial

WBOY
Release: 2016-07-13 10:55:42
Original
931 people have browsed it

The article introduces the difference between the three output functions echo, print and print_r. Let’s introduce the usage speed issue.

1.The difference between echo and print

The functions of echo and print in PHP are basically the same (output), but there are subtle differences between the two. There is no return value after echo output, but print has a return value, and it returns false when its execution fails. Therefore, it can be used as a normal function. For example, after executing the following code, the value of variable $r will be 1.

$r = print "Hello World";

This means that print can be used in some complex expressions, but echo cannot. However, because the echo statement does not require any value to be returned, the echo statement in the code runs slightly faster than the print statement.


echo has no return value; print has a return value, and the return value of print is always 1.

Expression
print can be used with complex expressions, echo cannot. For example, print can be used in the following example:

The code is as follows Copy code
< ?php$a=true;$a ? print "true":print "false";?>
 代码如下 复制代码

Parameters
echo can have multiple parameters, while print can only have one parameter.

echo If there are multiple parameters, they should be separated by commas. There is no need to add parentheses to each parameter. The correct writing method is as follows:

The code is as follows Copy code
echo "good ","for "," you";
 代码如下 复制代码
echo "good ","for ","you";

Note that if echo has multiple parameters, it is wrong to use only one parentheses to surround all parameters. The following writing is wrong:

The code is as follows Copy code
echo ("good ","for ", "you");
 代码如下 复制代码
echo ("good ","for ","you");

print can only have one parameter, such as:

The code is as follows Copy code
 代码如下 复制代码

print ("good for you");
print "good for you";

print ("good for you"); print "good for you";

The functions of echo and print are to output strings. The main difference between echo and print is that echo is faster than print because echo does not return a value.


The print_r() function is only used to output arrays.

The array contents output by the print_r function in php are not arranged. In order to make the output look better. For example, an array has multiple levels. Listed in sections, we can write like this:

Example #1 print_r() example

The code is as follows
 代码如下 复制代码

<br>
<?php<br />
$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));<br>
print_r ($a);<br>
?><br>

The above example will output:

<br>
Array<br>
(<br>
    [a] => apple<br>
    [b] => banana<br>
    [c] => Array<br>
        (<br>
            [0] => x<br>
            [1] => y<br>
            [2] => z<br>
        )<br>
)<br>

Copy code
<?php<p align="left">
$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));<div style="display:none;">
print_r ($a);<span id="url" itemprop="url">
?></span>
The above example will output:
</span>
Array<span id="isBasedOnUrl" itemprop="isBasedOnUrl">
(</span>
[a] => apple<span id="genre" itemprop="genre">
[b] => banana</span>
[c] => Array<span id="description" itemprop="description">
(</span>
                                                  [0] => x</div>
[1] = & gt; y 
                                                  [2] => z
)<div class="art_confoot">
)</div>
http://www.bkjia.com/PHPjc/632227.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632227.htmlTechArticleThe article introduces the difference between the three output functions echo, print and print_r. Let's introduce the usage speed issue. 1. The difference between echo and print The functions of echo and print in PHP are basically the same (...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!