user2816983
user2816983

Reputation:

how aright use function with echo and return?

Code ONE (WORK ARIGHT):

function Hello( $rel ) {
   $res = mysqli("SELECT * FROM TABLE");
   $result = $res->num_rows;
   if ( $rel == 1 ) {
      print $result;
   } elseif ( $rel == 2 ) {
      echo $result;
   } elseif ( $rel == 3 ) {
      return $result;
   } else { 
      return $result;
   }
}

$pr = HELLO(3);
echo $pr;

It code work aright.

Then I wanted to do one function to process the data and output the result.

Code:

function out( $rel, $result ) {
   if ( $rel == 1 ) {
      print $result; 
   } elseif ( $rel == 2 ) {
      echo $result;
   } elseif ( $rel == 3 ) {
      return $result;
   } else { 
      return $result;
   }
}

function Hello( $rel ) {
   $res = mysqli("SELECT * FROM TABLE");
   $result = $res->num_rows;
   out( $rel, $result )
}

$pr = HELLO(3);
echo $pr;

But now code not work(not show results on line echo $pr;)...

Tell me please why i have error and how write aright?

P.S.: i not know that need use return before function. Thanks all for my new knowledge.

Upvotes: 1

Views: 73

Answers (2)

kailash19
kailash19

Reputation: 1821

You have not return the value in the second code.

you need to use like this:

return out($rel,$result).

The return is in the second function, second function returns the value to function first, now function first also needs to return, so u need to add return there too.

Upvotes: 0

ariefbayu
ariefbayu

Reputation: 21979

You simply forgot to add return to out($rel,$result)

as it is right now, your Hello() function doesn't have return value.

Upvotes: 1

Related Questions