[solved] newbie: Return vs. Print
Posted: Tue Apr 12, 2005 4:34 am
Hi,
can anyone please explain, in layman's term, the use of RETURN?
thanks!
can anyone please explain, in layman's term, the use of RETURN?
thanks!
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
function add2numbers($num1, $num2)
{
$retval=$num1+$num2;
return $retval;
}
$myaddition=add2numbers(1,4);
echo($myaddition);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 />");
}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...
Code: Select all
$var = 'Hello World!';
echo $var;
//or this here
function mk_word() {
return "Hello World";
}
echo mk_word();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());
?>