What is the difference between PHP echo and PHP return in plain English?
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 echo
s 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)
Difference between echo and return in php?
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"
diference between return , echo and print keyword in PHP
return
is a language construct used to exit a function and give a value to the caller of the function.
echo
and print
are both language constructs that output strings. The main difference is that echo can take multiple arguments separated by commas, but print accepts only a single argument.
php echo vs return, which way is better?
Option 2 gives you the most flexibility when reusing the code. Next time you use it, you may not want to echo it out directly, but to perform other actions on it, store it for later etc
rest echo vs return in GET
Returning from a function does not actually render any content to the response. You may have seen people returning data in different frameworks but the framework is actually echoing the data for you behind the scenes.
what's the difference between return/echo json_encode in php
return
will not give any response in case of using ajax where as echo
will give you the response.If you are using server side scripting only the probably you dont need to echo it.Instead of it you can use return.
Look at this example and the way which the functions are called
function myFun($value){
return 'This is the value '.$value;
}
echo myFun(10);//Output This is the value 10
myFun(10);//Will not give you any output
function myFun($value){
echo 'This is the value '.$value;
}
myFun(10);//Output This is the value 10
echo myFun(10);//Output This is the value 10
In case of ajax
$.ajax({
url : 'mypage.php',
type : "POST",
success : function(res){
alert(res);
}
});
In mypage.php
echo 'Hello';//This will send Hello to the client which is the response
but
return 'Hello';//Will not send anything.So you wont get any response
Related Topics
PHP Echo VS PHP Short Echo Tags
Sum Values of Multidimensional Array by Key Without Loop
How to Properly Url Encode a String in PHP
PHP: Custom Error Handler - Handling Parse & Fatal Errors
Remove All Attributes from HTML Tags
Tracking the Script Execution Time in PHP
How to Truncate a String to the First 20 Words in PHP
Binding Multiple Values in Pdo
How to Remove All Non Printable Characters in a String
Passing $_Post Values With Curl
Get Table Column Names in MySQL
How to Upload Images into MySQL Database Using PHP Code
PHP With MySQL 8.0+ Error: the Server Requested Authentication Method Unknown to the Client
Special Characters in Property Name of Object
"Invalid Parameter Number: Parameter Was Not Defined" Inserting Data