What is the difference between echo and return in php

青灯夜游
Release: 2023-03-13 06:32:02
Original
3155 people have browsed it

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.

What is the difference between echo and return in php

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!

Related labels:
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