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"
What is the difference between return or echo the view? return view is talking longer then echo view
Laravel performs a lot of necessary actions after it gets the response from your controller. By exiting from your controller, you are terminating the request in the middle of its lifecycle:
- Terminable middleware will not be run. Middleware in most frameworks can run before the request is sent to the controller and after the response is received from the controller.
- Terminating callbacks (registered in the application/container) will not be executed.
- Session may not be persisted. Depending on the driver being used, session data may not be persisted until after the controller returns a response.
- All of your cookies and headers may not be sent.
- The response will not be automatically converted to a string or JSON.
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.
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.
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
Pdoexception' with Message 'Sqlstate[22001]: String Data, Right Truncated: 0
Check If Http Request Comes from My Android App
Converting a Byte Array into an Image Using PHP and HTML
How to Get the List of Available Locales in PHP
Pass Form Data to Another Page with PHP
Find All Hrefs in Page and Replace with Link Maintaining Previous Link - PHP
PHP - How to Get Shell Errors Echoed Out to Screen
How to Sftp Upload Files from PHP
What Is the Easiest Way to Use the Head Command of Http in PHP
PHP - Fastest Way to Check Presence of Text in Many Domains (Above 1000)
Properly Escaping with MySQLi | Query Over Prepared Statements
Interpreting Return Value of Function Directly as an Array
How to Convert an Integer to an Array in PHP
Simulate PHP Array Language Construct or Parse with Regexp