Home >Backend Development >PHP Problem >What is the difference between echo and return in php
Difference: 1. Echo outputs a string to the browser, while return terminates the execution of the following code and returns a value; 2. Echo is mainly used for PHP's output to the browser, while return is mainly used for Handle the return value of the function.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
The problems encountered in the project were originally intended to The controller directly returns $xml ($xml is a piece of code in xml format), but the test found that nothing was output. Replaced it with echo $xml; and it was successful, so I went to Baidu to check the difference between echo and return in php:
1. From the perspective of keyword meaning:
echo is to output a string to the browser
return is mainly used for functions, returning a value
2, function
echo is mainly used for: php's output to the browser;
return is mainly used to process the return value of the function
Note:
In most cases, echo and The result of return is the same. But if there are nested functions or too many functions, echo will be invalid or an error will occur. Therefore, it is better to use return when processing the return or output of a function.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the difference between echo and return in php. For more information, please follow other related articles on the PHP Chinese website!