Jump to content

Recommended Posts

Hi.

 

I'm new to PHP (I'm just learning about functions today), and I have a question regarding "echo" and "return". I do understand that "return" is intended to be used within a function and it seems that it's only intended for use within a function. However, the following is what's puzzling me.

 

This code is from W3C Schools:

 

<?php
function add($x,$y)
  {
  $total = $x + $y;
  return $total;
  }

echo "1 + 16 = " . add(1,16);
?>

 

And it outputs as "1 + 16 = 17" as it should, and I understand why it works.

 

But why can't I use "echo $total;" in the function instead of "return $total;"?

 

Like this:

 

<?php
function add($x,$y)
  {
  $total = $x + $y;
  echo $total;
  }

echo "1 + 16 = " . add(1,16);
?>

 

I've tried this, but the output from this is "171 + 16 ="

 

Yes, using return does work. I'm not asking for a coding solution, I'm merely asking for the logic behind this that makes it function the way it does.

 

I would appreciate it if anyone could take the time to give me some tips. :) PHP is something I've always wanted to learn, but I want to LEARN it, not just memorize it... so that's why I'm asking this.

echo is an output feature. Sort of like a writeln if you use any other languages. return is simple a return of the value. It is only output because you are calling the return command from an echo statement.

 

Hence $value=add(1,16); would do nothing except return the value. Unless the function had an echo in it.

 

You see 171 because the function script is finished and echos before the main echo command is finished.

echo prints something on screen and return gives a value...

 

 

<?php
function addEm()
{
      $result = 1+2;

   return $result
}

$theVar = addEm();

if($theVar => 3)
{
   echo 'It is at least 3';
}
?>

 

Yes in a lot of instances you may want to echo what is returned, but in a lot of instances, you will want to use that value in a comparative or operational sense.

 

Say you have a credit card mod10 check function. (mod10 is an algorithim for determining if a CC num is valid)

 

You will generally not want to echo the result, but compare it to a true/false or 1/0 type value.

 

So in short, echo will print on screen, return gives a value that you can set in a var and use it for echo or comparative operations.

 

Hope this helps.

Thank you, to all three of you. ;D

 

I just got the little light-bulb over my head and went "Ooooohhhh." I have a much better understanding of the logic behind this now, what is exactly what I was hoping to achieve.

 

I love this site. You guys are awesome. :)

doing what you did, your pretty much echoing the result twice, it's the same as writing

function add($x,$y)
  {
  $total = $x + $y;
  echo $total;
  }

echo "$total" . add(1,16);

 

 

Nothing against your code timmah, or what your saying... this is for original posters clarification....

 

In your example, $total is not available to the code as it is set inside the function and outside the scope of the operation.

 

So echo $total . add(1,16); essentially is <undefined variable > . add(1,16);

 

$total has no value unless it is set outside the function, or is made available as a global.

 

So to do what you wrote, the function would have to look like this.....

 

 

function add($x,$y)
{
  global $total;

  $total = $x + $y;
  echo $total;
}

echo "$total" . add(1,16);

 

And then, it would not echo 1 + 16, it would echo 17 as $x + $y will perform the arithmetic and set $total to the value of $x + $y.

 

Functions are pretty handy and can be a HEADACHE if you don't understand the scope of variables and such.

 

Hope this makes sense to all...... sorry for posting after it has been marked solved.... just saw this and thought I would give my 1.5 cents

Nate

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.