Home > Article > Backend Development > The difference between echo, print and print_r function_PHP tutorial
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";
|
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");
|
print can only have one parameter, such as:
The code is as follows | Copy code | ||||
|
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
|
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 (...