Re: Differences
| From: | Rosalind Wills | Date: | Thu, 04 Oct 2012 03:13:35 +0000 |
| Subject: | Re: Differences | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-319354@lists.php.net to get a copy of this message | ||
On 10/3/12 9:57 PM, David McGlone wrote:
Absolutely. I also think I learned that return can also work like echo if the code is written correctly.Echo and return are two completely different things in PHP. Echo is used for printing a value out in a document -- for instance, as follows, in the midst of a chunk of HTML: <table>
<tr><td>Name:</td><td><?php echo $name; ?></td><tr>
<tr><td>Email:</td><td><?php echo $email; ?></td></tr>
</table>
In the above example, echo is being used to output the value specified in the variable $name or $email (which would have been previously set)
It can also be used to print the return value of a function. Return, in and of itself, does not print anything; it is used to ascribe a particular value to an instance of a function.
So for instance, if you have a function:
function getEmail() {
$name = 'johnsmith';
$email = $name . '@sample.com';
return $email;
}
then any called instance of the function getEmail() will, in a sense, have the value of the return statement (in a sense, the code inside the function runs and *returns* a value in place of itself).
So
$variable = getEmail();
//variable is now equal to 'johnsmith@sample.com'
You could then echo this value, if you wanted to.
echo getEmail(); //prints the value returned by getEmail()