acctman
acctman

Reputation: 4349

calling function, echoing results

I'm trying to get this function to work where it returns the file location on the server. at the bottom of the coding I show how i'm trying to display the coding in my other pages. Am i right when echo'ing the last line of coding to get it to display when calling the function?

function record_loc($id,$type) 
{
  $recdir = mysql_result(mysql_query(
      "SELECT rec_loc FROM names WHERE com_id = '".$id."'"), 0 , "rec_loc");

  $ver = ($type == 'n' ? '-n.wav' : '-s.wav');
  // echo '/doc_rec/' . $recdir . '/' . $id . $ver;
return '/doc_rec/' . $recdir . '/' . $id . $ver;
} 

// echo record_loc(120,n); or <?= record_loc(120,n) 
?>

Upvotes: 0

Views: 116

Answers (4)

Tom Vervoort
Tom Vervoort

Reputation: 534

Or you return the path and use echo record_loc(120,n);

Or you echo the path in the return (like the sample) and use <?php record_loc(120,n) ?>

Upvotes: 1

Brad Christie
Brad Christie

Reputation: 101604

Your example would, in fact, output the data (just due to simplicity of the statement), however what's really happening it the function is echoing first, then the echo [commented in this case] is performed. e.g.

function return_some_value(){
  return 'abc';
}
function echo_some_value(){
  echo 'def';
}

echo return_some_value() . echo_some_value();

output would result in echo_some_value displaying out first (and returning null) which is then passed to echo for further execution. e.g.

defabc

(though you'd expect abcdef)

EDIT per request in comment to this Answer EDIT

function record_loc($id,$type) 
{
  if (($query = mysql_query("SELECT rec_loc FROM names WHERE com_id = '".(int)$id."'")) !== false)
  {
    if (mysql_num_rows($query) > 0)
      return '/doc_rec/'.mysql_result($query,0,'rec_loc').'/'.$id.'-'.($type=='n'?'n':'s').'.wav';
  }
  return 'Error Not Found';
}

Please note you should be checking your connection, checking for sql errors and validating input. I have not exhausted all the checks, just making your code do what what you requested.

Upvotes: 2

SW4
SW4

Reputation: 71150

There is a functional difference between using return and an output operator like echo, print, print_r, sprint etc.

See here for return and here for echo

Namely:

'return' returns its argument as the value of the function call

'echo' outputs one or more strings

So, return in your function passes the result as a value you can give to a variable, i.e. $myvar=myfunction();, you would then need to echo $myvar (or do echo myfunction();), whereas with echo in your function, simply having myfunction(); would accomplish the same. Use return when you want to pass the result of a function through other operations.

Upvotes: 3

demux
demux

Reputation: 4654

I'm not sure if I understand the question. But you can't echo a function if it doesn't return anything.

Here is a simple function that returns a value:

<?php
function do_stuff() {
    return rand(1, 10);
}

echo do_stuff();
?>

<?= do_stuff() ?>

Upvotes: 3

Related Questions