Hi,
can anyone please explain, in layman's term, the use of RETURN?
thanks!
[solved] newbie: Return vs. Print
Moderator: General Moderators
[solved] newbie: Return vs. Print
Last edited by gen23 on Thu Apr 14, 2005 10:04 am, edited 1 time in total.
- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
How layman do you want it...
Simplistic Examples
Would print out 5.
Going by the title of the post, return values actually return a "result" of the function which is normally saved in a variable ($myaddition in the example above). Printing/echo may be performed within the function in addition to the return. Often returns are used for error handling. If function returns 0 rather than 1 the function failed for some reason example.
Ok very simplistic examples but I hope you can get the idea.
Simplistic Examples
Code: Select all
function add2numbers($num1, $num2)
{
$retval=$num1+$num2;
return $retval;
}
$myaddition=add2numbers(1,4);
echo($myaddition);Going by the title of the post, return values actually return a "result" of the function which is normally saved in a variable ($myaddition in the example above). Printing/echo may be performed within the function in addition to the return. Often returns are used for error handling. If function returns 0 rather than 1 the function failed for some reason example.
Code: Select all
function isNumberValid($num)
{
$retval=0;
if ($num>0 and $num<=10) {
echo("I don't know some silly text");
$retval=1;
}
return $retval;
}
if (isNumberValid($_REQUEST['id']==0) {
echo("The number you entered must be between 1 and 10 <br />");
}I see... but is it okay to use Return in displaying the contents of a variable just like print/echo? or it's just simply for saying that a value is being returned?
ex.
--|
<?php |
$msg = "hello!"; |--- why isn't this block of code used/allowed?
return $msg; |
?> __|
...Sorry... I'm really in the dark here...
ex.
--|
<?php |
$msg = "hello!"; |--- why isn't this block of code used/allowed?
return $msg; |
?> __|
...Sorry... I'm really in the dark here...
Last edited by gen23 on Wed Apr 13, 2005 10:10 am, edited 1 time in total.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
return is used to tell a function/process what value to send back. echo /print (aliases of each other) are used to output something to the page.gen23 wrote:I see... but is it okay to use Return in displaying the contents of a variable just like print/echo? or it's just simply for saying that a value is being returned?
ex.
<?return "hello";?> <-- why isn't this allowed?
...Sorry... I'm really in the dark here...
Even simpler example:
Code: Select all
$var = 'Hello World!';
echo $var;
//or this here
function mk_word() {
return "Hello World";
}
echo mk_word();- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
This isn't allowed as return is only allowed to pass values out of a function. It also stops the rest of the function processing.gen23 wrote: <?php
$msg = "hello!";
return $msg;
?>
Code: Select all
<?php
function testit()
{
return "Hello";
return "Never";
}
echo(testit());
?>