Re: Differences

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxx'

You could then echo this value, if you wanted to.

echo getEmail(); //prints the value returned by getEmail()

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux