[solved] newbie: Return vs. Print

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gen23
Forum Newbie
Posts: 7
Joined: Fri Apr 08, 2005 10:13 am

[solved] newbie: Return vs. Print

Post by gen23 »

Hi,

can anyone please explain, in layman's term, the use of RETURN?

thanks!
Last edited by gen23 on Thu Apr 14, 2005 10:04 am, edited 1 time in total.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

How layman do you want it...

Simplistic Examples

Code: Select all

function add2numbers($num1, $num2)
{
  $retval=$num1+$num2;
  return $retval;
}

$myaddition=add2numbers(1,4);
echo($myaddition);
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.

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 />");
}
Ok very simplistic examples but I hope you can get the idea.
gen23
Forum Newbie
Posts: 7
Joined: Fri Apr 08, 2005 10:13 am

Post by gen23 »

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... :oops:
Last edited by gen23 on Wed Apr 13, 2005 10:10 am, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

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... :oops:
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.

Even simpler example:

Code: Select all

$var = 'Hello World!';
echo $var;

//or this here
function mk_word() {
    return "Hello World";
}
echo mk_word();
So either way... you need to use echo /print to output the value to the page.
gen23
Forum Newbie
Posts: 7
Joined: Fri Apr 08, 2005 10:13 am

Post by gen23 »

WOW!!! I got it now!!! Thank you! Thank You!!:D

.... unfortunately... I still have a long, long way to go... :cry:
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

gen23 wrote: <?php
$msg = "hello!";
return $msg;
?>
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.

Code: Select all

<?php
  function testit()
  {
    return "Hello";
    return "Never";
  }

  echo(testit());
?>
Would only ever show "Hello".
gen23
Forum Newbie
Posts: 7
Joined: Fri Apr 08, 2005 10:13 am

Post by gen23 »

I see.... Thank you! Thank you!
Post Reply