This so confusing me ,What is the difference between the echo and return, in functions
-
http://php.net/manual/en/functions.returning-values.php ... do you understand the purpose of a function? – Felix Kling Jun 05 '12 at 19:32
-
3Every language has an equivalent of the two. Think in terms of any other language you know. `echo` prints, `return` returns – Ayush Jun 05 '12 at 19:32
-
6OMG what a question from someone named `PHP`! – anubhava Jun 05 '12 at 19:34
-
@anubhava - LOL! Didn't even notice that! – Jim Jun 05 '12 at 19:35
-
@PHP: yes, none of us know the answer, so we're trying to change the topic. – Ayush Jun 05 '12 at 19:40
-
The return() statement returns any ensuing value back to the function caller, returning program control back to the caller’s scope in the process. – PHP Jun 05 '12 at 19:43
-
function salestax($price,$tax=.0575) { $total = $price + ($price * $tax); return $total; } you will get an idea about function – Ballu Rocks Jun 05 '12 at 19:48
-
3The question demonstrates a fundamental lack of knowledge of programming, whether PHP or otherwise. `echo` and `return` are utterly different in what they do. Answers to this question here are not going to be sufficient to teach the basics of programming that are required to give the understanding that is missing here. – Spudley Jun 05 '12 at 19:48
-
@Spudley - Notice how nXqd below was able to answer the question without resorting to the stereotypical condescension that SO is known for. I suggest, in the future, to try and intuit (just a little) at what someone's confusion is. The return vs echo confusion is common among noobs requiring nothing more than a one sentence answer. You'll be a far greater asset to the community by understanding where noob pitfalls are. If you need an interpreter, I'm willing to volunteer. – Padawan Jan 12 '17 at 19:10
-
@Padawan Not sure what prompted you to respond to me five years after it was posted, but it's pretty safe to say that all involved in this question have moved on since then. Your comment is a reasonable criticism and I accept it, but is a little late to be helpful. – Spudley Jan 12 '17 at 19:39
-
@Spudley - My response is because I had the same problem, this page came up as a solution. Though you've "moved on" SO pages stay in SERPs to provide help for people who will also come across the same problem. Kind of like a sign on a hiking trail from 5 years back, "Dead End, don't go this way, go 'that' way instead". My motivation is also based on condescending answers that provide no help to those who need it. Counter productive. My goal is to help make SO more helpful to noobs; currently, it's not noob friendly. – Padawan Jan 12 '17 at 19:48
5 Answers
echo
outputs content to the console or the web browser.
Example:
echo "Hey, this is now showing up on your screen!";
return
returns a value at the end of a function or method.
Example:
function my_function()
{
return "Always returns this";
}
echo my_function(); // displays "Always returns this"

- 1,129
- 7
- 32
-
-
-
-
7**My goodness, I can help you no further.** Please... _actually_ take the time to read and comprehend the other answers. – Deltik Jun 05 '12 at 20:18
-
4@PHP `Return` ends the execution of the function while `echo` does not – user20152015 Feb 01 '18 at 13:43
echo - Output one or more strings
return - If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call. return will also end the execution of an eval() statement or script file.
Take your time and read php manual instead.

- 9,152
- 14
- 65
- 104
-
4This is what I was looking for. While all the other answers were condescending, this one gets to the heart of why noobs are confused. Which is, the difference within the function. They both can result in output to a browser, but the return kills any further progression in a loop. It's not so much not reading the manual, as it is a normal confusion that comes up among beginners that can be easily answered, such as here. – Padawan Jan 12 '17 at 19:06
-
-
@Dzung How to handle the return as ajax response without using echo and echo json_encode() – Muhammad Tarique Feb 29 '20 at 12:21
Ah...
There's a HUGE difference.
In basic:
return $a
returns a value from the function or ends the functionecho $a
outputs a valuefunction foo() { return 5; } $x = foo(); // $x holds the value 5 echo $x; // outputs "5"

- 10,676
- 16
- 68
- 122
Echo prints strings to the screen or the browser. Return ends the function, optionally sending a value back from the function to the code that called the function.

- 29,840
- 14
- 57
- 64
-
effectively it ends the function but it also returns a specified value from the function – CrayonViolent Jun 05 '12 at 19:32
-
1not neccessarly. You could do a `function foo() {return;}` which is basically a void function. – Johannes Klauß Jun 05 '12 at 19:35
-
One more difference is that while ECHO can be used either inside or outside a function, but RETURN needs to be used always inside a function. – limakid Aug 07 '22 at 07:09
Echo allows you to send a value to the browser, for display to the user.
Return allows you to end a function, as well as pass off a value to another function or variable.
Check out this link, which goes into more detail:

- 2,300
- 1
- 19
- 43
-
check this example ,you will get an idea, function salestax($price,$tax=.0575) { $total = $price + ($price * $tax); return $total; } – Ballu Rocks Jun 05 '12 at 19:47