Differences between echo and print functions and print_r functions. This article introduces the differences between the three output functions echo and print and print_r. next we will introduce the usage speed. 1. differences between echo and print: echo and print functions are basically the same in PHP (This article introduces the differences between echo and print functions and print_r. next we will introduce the usage speed.
1. differences between echo and print
The echo and print functions in PHP are basically the same (output), but there are still slight differences between the two. No return value after echo output, but print has a return value. if the execution fails, flase is returned. Therefore, it can be used as a common function. for example, after the following code is executed, the value of $ r is 1.
$ R = print "Hello World ";
This means that print is available in some complex expressions, but echo is not. However, because the echo statement does not require any value to be returned, the running efficiency of the echo statement in the code is slightly faster than that of the print statement.
Echo does not return values; print has a return value, and print always returns 1.
Expression
Print can be used for complex expressions, but echo cannot. For example, print can be used as follows:
Parameters
Echo can have multiple parameters, while print can have only one parameter.
If multiple parameters exist in echo, separate them with commas (,). you do not need to add parentheses for each parameter. the correct syntax is as follows:
The code is as follows: |
|
Echo "good", "for", "you ";
|
Note: If echo has multiple parameters, it is incorrect to enclose all parameters with only one parentheses. The following statement is incorrect:
The code is as follows: |
|
Echo ("good", "for", "you ");
|
Print can have only one parameter, for example:
The code is as follows: |
|
Print ("good for you "); Print "good for you "; |
Echo and print are both output strings. The main difference between echo and print is that echo is faster than print because echo has no return value.
Print_r () function, used only to output arrays.
In php, the content of the arrays output by the print_r function is not arranged. To make the output look better. For example, arrays have multiple layers. To list segments, we can write as follows:
Example #1 print_r () example
The code is as follows: |
|
$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z')); print_r ($a); ?>
The above example will output: Array ( [a] => apple [b] => banana [c] => Array ( [0] => x [1] => y [2] => z ) )
|
Bytes. 1. differences between echo and print: echo and print functions in PHP are basically the same (...