2

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.

3
  • 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. Commented Feb 10, 2012 at 15:35
  • I don't understand exactly your question. Can you be more clear? Commented Feb 10, 2012 at 15:35
  • What should the output be ? onetwothreefoursomething additional ?? Commented Feb 10, 2012 at 15:42

4 Answers 4

6

Yes, you just need

return myFunction();
Sign up to request clarification or add additional context in comments.

Comments

0

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";
}

Comments

0
function myfunction2() {

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

  //You might want to do something else here

 return 'something additional';
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.