What is the difference between PHP echo and PHP return in plain English?

php function return
what is the differences between a php echo and a php print statement
php return type mixed
php echo multiple lines
php return multiple values
php return false
php function return type
php return array

Yes, I have googled this question and even referred to my textbook (PHP by Don Gosselin) but I seriously can't seem to understand the explanation.

From my understanding:

echo = shows the final result of a function

return = returns the value from a function

I applied both echo and return in the following functions I can't see the difference or the 'effectiveness' of using return instead of echo.

<?php
echo "<h1 style='font-family:Helvetica; color:red'>Using <em>echo</em></h1>";
function add1($x, $y){
    $total = $x + $y;
    echo $total;
}
echo "<p>2 + 2 = ", add1(2, 2), "</p>";

echo "<h1 style='font-family:Helvetica; color:red'>Using <em>return</em></h1>";
function add2($x, $y){
    $total = $x + $y;
    return $total;
}
echo "<p>2 + 2 = ", add2(2, 2), "</p>";

?>

Both display the result! What am I not understanding?

I'm going to give a completely non-technical answer on this one.

Let's say that there is a girl named Sally Function. You want to know if she likes you or not. So since you're in grade school you decide to pass Sally a note (call the function with parameters) asking her if she likes you or not. Now what you plan on doing is asking her this and then telling everyone what she tells you. Instead, you ask her and then she tells everyone. This is equivalent to returning (you getting the information and doing something with it) vs her echoing (telling everyone without you having any control).

In your case what is happening is that when Sally echos she is taking the control from you and saying "I'm going to tell people this right now" instead of you being able to take her response and do what you wanted to do with it. The end result is, however, that you were telling people at the same time since you were echoing what she had already echoed but didn't return (she cut you off in the middle of you telling your class if she liked you or not)

PHP Function Return Statement & difference between Echo vs Return, any type of data, such as string, number, array, boolean, etc. The most important difference between echo and return in my viewpoint is: the data type of result for each one. when we write some functions like below:

Consider the following:

<?php
function sayHelloLater(){
    return "Hello";
}

function sayGoodbyeNow(){
    echo "Goodbye";
}

$hello = sayHelloLater(); // "Hello" returned and stored in $hello 
$goodbye = sayGoodbyeNow(); // "Goodbye" is echo'ed and nothing is returned

echo $hello; // "Hello" is echo'ed
echo $goodbye; // nothing is echo'ed
?>

You might expect the output to be:

HelloGoodbye

The actual output is:

GoodbyeHello

The reason is that "Goodbye" is echo'ed (written) as output, as soon as the function is called. "Hello", on the other hand, is returned to the $hello variable. the $goodbye is actually empty, since the goodbye function does not return anything.

PHP Echo and Print Statements, What is the difference between echo and print in PHP? What is the difference between Echo and Return? The PHP echo statement is used to output one or more strings and the PHP Function Return statement is used to return any type of data, such as string, number, array, boolean, etc.

I see you are posting comments still which suggest you are confused because you don't understand the flow of the code. Perhaps this will help you (particularly with Mathias R. Jessen's answer).

So take these two functions again:

function sayHelloLater() {
    return 'Hello';
}

function sayGoodbyeNow() {
    echo 'Goodbye';
}

Now if you do this:

$hello = sayHelloLater();
$goodbye = sayGoodbyeNow();

echo $hello;
echo $goodbye;

You will be left with 'GoodbyeHello' on your screen.

Here's why. The code will run like this:

$hello = sayHelloLater();  ---->-------->-------->------->------>--
                                                                  ¦
  ¦           ^                                                   ¦
  ¦           ¦                                           Call the function
  v           ¦                                                   ¦
  ¦           ^                                                   ¦
  ¦           ¦                                                   v
  ¦
  v         "return" simply sends back                 function sayHelloLater() {
  ¦          'Hello' to wherever the     <----<----        return 'Hello';
  ¦             function was called.                   }
  v           Nothing was printed out
  ¦          (echoed) to the screen yet.
  ¦
  v

$hello variable now has whatever value
the sayHelloLater() function returned,
so $hello = 'Hello', and is stored for
whenever you want to use it.

  ¦
  ¦
  v
  ¦
  ¦
  v

$goodbye = sayGoodbyeNow();  ---->-------->-------->------->------
                                                                 ¦
  ¦              ^                                               ¦
  ¦              ¦                                       Call the function
  v              ¦                                               ¦
  ¦              ^                                               ¦
  ¦              ¦                                               v
  ¦              ¦
  v              ¦                                    function sayGoodbyeNow() {
  ¦                                                       echo 'Goodbye';
  ¦        The function didn't return                 }
  ¦        anything, but it already
  v         printed out 'Goodbye'                                ¦
  ¦                                                              v
  ¦           ^
  ¦           ¦                                    "echo" actually prints out
  v           <-----------<-----------<---------     the word 'Goodbye' to
  ¦                                                 the page immediately at
  ¦                                                       this point.
  ¦
  v

Because the function sayGoodbyeNow() didn't
return anything, the $goodbye variable has
a value of nothing (null) as well.

  ¦
  ¦
  ¦
  v
  ¦
  ¦
  ¦
  v

echo $hello;  -------->------->   Prints 'Hello' to the screen at
                                  this point. So now your screen says
  ¦                               'GoodbyeHello' because 'Goodbye' was
  ¦                               already echoed earlier when you called
  ¦                               the sayGoodbyeNow() function.
  v

echo $goodbye;  ------>------->   This variable is null, remember? So it
                                  echoes nothing.
  ¦
  ¦
  ¦
  v

And now your code is finished and you're left with
'GoodbyeHello' on your screen, even though you echoed
$hello first, then $goodbye.

PHP echo Function, Getting Started · Introduction · A simple tutorial · Language Reference · Basic syntax If called from the global scope, then execution of the current script file is ended. Note: Note that since return is a language construct and not a function, the echo "a"; ?> b.php <?php echo "b"; return; ?> (executing a.php:) will echo "​ba". PHP echo and print Statements. echo and print are more or less the same. They are both used to output data to the screen. The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions. echo can take multiple parameters (although such usage is rare) while print can take one argument.

with return the function itself can be treated somewhat like a variable.

So

return add1(2, 3) + add1(10, 10);

will output:

25

while

echo add2(2, 3) + add2(10, 10);

will output:

5
20
0

As there is no result of add2. What it does is only echo'ing out stuff. Never actually returning a value back to the code that called it.

Btw, you are not dumb. You are just a beginner. We are all beginners in the beginning, and there is a certain threshold you'll need to get over in the beginning. You will probably have a lot of "dumb" questions in the beginning, but just keep on trying and above all experiment, and you will learn.

What does php's echo do?, echo is not actually a function (it is a language construct), so you are not required to use does not behave like a function, so it cannot always be used in the context of a function. foo. The major differences to print are that echo accepts an argument list and doesn't have a return value. There are two notable differences. Mathias R. Jessen. top 0.06% this year 46 What is the difference between PHP echo and PHP return in plain English? English Language Learners; Japanese Language;

So, basically you’ll want to use echo whenever you want to output something to the browser, and use return when you want to end the script or function and pass on data to another part of your script.

return - Manual, Change language: echo square(4); // outputs '16'. ?> The same types are available for return type declarations as are available for argument type declarations. As of PHP 7.1.0, return values can be marked as nullable by prefixing the type Return values of a type different to that specified are silently converted with  Outputs all parameters. No additional newline is appended. echo is not actually a function (it is a language construct), so you are not required to use parentheses with it. echo (unlike some other language constructs) does not behave like a function, so it cannot always be used in the context of a function.

echo - Manual, Functions are expressions with the value of their return value. Since foo() PHP is an expression-oriented language, in the sense that almost everything is an expression. Consider the The difference is with the value of the increment expression. echo $b."\n"; ?> Example (works): <?php for ($a = 2, $b = 4; $a < 3; $a++) is a new line feed within the context of plain text, while <br /> is line break within the context of HTML.. HTML can interpret when using preformatted blocks (e.g. <pre>), but it does not by default, and should not unless there is a specific use case (like when quoting, citing poetry, or showing code).

Returning values - Manual, How to pass form variables from one page to other page in PHP ? Web Scrapping in PHP Using Simple HTML DOM Parser · How to make a spider-bot print: It is not a real function. it is a language construct but always returns the value 1. PHP files can contain text, HTML tags and scripts. PHP files are processed by the web server and returned back to the browser as plain HTML. PHP files can be identified with the file extensions “.php”, “.php3”, or “.phtml” Difference between HTML and PHP

Expressions - PHP: Expressions, In this tutorial you will understand the basic difference between PHP echo and Since echo is a language construct not actually a function (like if statement), you can <?php // Displaying HTML code echo "<h4>This is a simple heading. way except that the print statement can only output one string, and always returns 1. Difference between Prim's and Kruskal's algorithm for MST; Difference between Argument and Parameter in C/C++ with Examples; Difference between Increment and Decrement Operators; Difference between 2NF and 3NF in DBMS; What are the differences between Bellman Ford's and Dijkstra's algorithms? Difference between Clustered and Non-clustered index

Comments
  • i think the definition of echo is Output one or more strings, that definition itself is clear enough source: php.net/manual/en/function.echo.php
  • I always think of the "return" statement as a way to export data so that it can be reused. Returned data is delivered in its "raw" format. As for "echo", the data is always a string (even numbers) because it is targeting the computer monitor (or screen) which only accepts string values.
  • You couldn't have explained it simpler! Although I still have a bit of difficulty trying to understand why I can't just echo within the function and then echo the function I do understand what goes behind the scenes between echo and return thanks to your analogy :D
  • Consider if you were wanting to save the results in variable so you could use it later. If your function echos instead of returning, you aren't actually getting anything to save in the variable.
  • Oh I see!!!! I think I'm finally getting it :-D … Now I just need to know why the results from an echo and return could be different lol (see @Aron Cederholm's post below) Thank you very much jprofitt — I appreciate your help!
  • what is a better programming practise to use echo or return in php functions? What is more secure ?
  • Thanks explained it well
  • I see what is going on over here but don't know 'why' ! I mean, why in the first instance it IS adding the results but the second one doesn't :-S
  • A function is much lika a magic box. You put something in it (parameters) and pull a lever, and something either happens or something is returned. E.g. you put an apple in a black box, and press start. After a lot of sounds and noises the box suddenly plants an apple tree in the ground which instantly grows to full size. This is what the black box/function performs. But when you open box there is not an apple left but only an apple crot. This is what the function returns.
  • Now, consider add1 and add2. Add1 is a black box with two input trays on the front. In the first tray you put a huge cage of apples, too many for you to count. In the second tray you put another huge cage of apples, also too many to count. When you press start you hear some noice and from the back of the box comes a cage of all apples combined (91.218 apples in all).
  • Then you put two other cages of apple in the incoming trays of the add2 black box. There is a lot of sound and then nothing comes out of the black box. Instead the black box produces an arm that writes "91.218" on a white board nearby.
  • Now consider that you are really the computer. You are a blind robot that only does what you are told to do. In the first example you can simply take the resulting cage of apples and count them and you will instantly know (because you are so fast) that there are 91.218 apples in the cage. But in the other example, you didn't get a return cage and as you are blind you really have no clue what the black box wrote on the white board.
  • This does not provide an answer to the question.