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:
The code is as follows: |
Copy code |
<Html> <body> <? Php $ a = true; $? Print "true": print "false";?> </Body> |
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: |
Copy code |
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: |
Copy code |
Echo ("good", "for", "you ");
|
Print can have only one parameter, for example:
The code is as follows: |
Copy code |
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: |
Copy code |
<Pre> <? Php $ A = array ('a' => 'apple', 'B' => 'bana', 'C' => array ('X', 'y ', 'Z ')); Print_r ($ ); ?> </Pre> The above example will output: <Pre> Array ( [A] => apple [B] => banana [C] => Array ( [0] => x [1] => y [2] => z ) ) </Pre> |