Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the difference between echo and return in php

Shulou Source: shulou.com Published: 2022-06-03 09:45:27 10月03日 Update

What is the difference between echo and return in php? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Differences: 1, echo is the output string to the browser, while return is to terminate the execution of the following code and return a value; 2, echo is mainly used for the output of php to the browser, while return is mainly used to deal with the return value of the function. Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

The problem encountered in the project was to return $xml directly in the controller ($xml is a piece of code in xml format), but the test found that nothing was output. Replace it with echo $xml;, so Baidu specially took a look at the difference between echo and return in php:

1. In terms of the meaning of key words:

Echo is the output string to the browser

Return is mainly used for functions and returns a value

2. Function

Echo is mainly used for: output of php to browser

Return is mainly used to handle the return value of a function.

Description:

In most cases, the results of echo and return are the same. However, if functions are nested or there are too many functions, echo will be invalid or an error will occur. So it's better to use return to handle the return or output of the function.

The answer to the question about the difference between echo and return in php is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Tags: Function output browser question browse process code character string situation more help answer easy successful easy function key keyword content Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB NVidia Apple Redmi OPPO Reno