Wordpressor
Wordpressor

Reputation: 7553

PHP - passing functions return to another function

I have a PHP function that returns something:

function myfunction() {
   $array = array('one', 'two', 'three', 'four');

   foreach($array as $i) {
     echo $i;
   }
}

And another function where I want to pass return values from the function above:

function myfunction2() {
   //how to send myfunction()'s output here? I mean:
   //echo 'onetwothreefour';
   return 'something additional';
}

I guess it will look something like myfunction2(myfunction) but I don't know PHP too much and I couldn't make it work.

Upvotes: 2

Views: 12436

Answers (4)

Manse
Manse

Reputation: 38147

Try this :

function myfunction() {
   $array = array('one', 'two', 'three', 'four');
   $concat = '';
   foreach($array as $i) {
     $concat .= $i;
   }
   return $concat;
}

function myfunction2() {
   return myfunction() . "something else";
}

this will return onetwothreefoursomthing else

Working example here http://codepad.org/tjfYX1Ak

Upvotes: 0

Eugen Rieck
Eugen Rieck

Reputation: 65342

function myfunction2() {

  $myvariable=myfunction();
  //$myvar now has the output of myfunction()

  //You might want to do something else here

 return 'something additional';
}

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324810

myfunction will always return "one". Nothing else. Please revise the behaviour of return

After that, if you still want the return value of one function inside another, just call it.

function myfunction2() {
    $val = myfunction();
    return "something else";
}

Upvotes: 0

rooftop
rooftop

Reputation: 3027

Yes, you just need

return myFunction();

Upvotes: 6

Related Questions