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 like myfunction2(myfunction) but I don't know PHP too much and I couldn't make it work.

share|improve this question

2  
Using return in a foreach does'nt sound right. Because it will return once and exit the function, so it will not get to the next array item. – Orhan Feb 10 at 15:35
I don't understand exactly your question. Can you be more clear? – DonCallisto Feb 10 at 15:35
My fault, I meant "echo"! – Wordpressor Feb 10 at 15:35
What should the output be ? onetwothreefoursomething additional ?? – ManseUK Feb 10 at 15:42
feedback

4 Answers

up vote 0 down vote accepted

Yes, you just need

return myFunction();
share|improve this answer
feedback

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";
}
share|improve this answer
feedback
function myfunction2() {

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

  //You might want to do something else here

 return 'something additional';
}
share|improve this answer
feedback

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

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.