monkeytooth Posted February 19, 2010 Share Posted February 19, 2010 I want to build a function that has multiple outputs. I know but am not to sure how I should go about that, the only thing I know won't work is return a; return b; return c; that will return one result at best. what I need to do is output a b and c from the same function, but I am looking for ideas on how to do that. Link to comment https://forums.phpfreaks.com/topic/192609-building-a-function-with-multiple-outputs/ Share on other sites More sharing options...
jl5501 Posted February 19, 2010 Share Posted February 19, 2010 If you do actually need to return more than one value, then you can place the values into an array and return that. Link to comment https://forums.phpfreaks.com/topic/192609-building-a-function-with-multiple-outputs/#findComment-1014725 Share on other sites More sharing options...
monkeytooth Posted February 19, 2010 Author Share Posted February 19, 2010 what like? $myVars = array($a, $b, $c); return $myVars; so when I call my function() in else where how would I handle the array it forms? Link to comment https://forums.phpfreaks.com/topic/192609-building-a-function-with-multiple-outputs/#findComment-1014731 Share on other sites More sharing options...
jl5501 Posted February 19, 2010 Share Posted February 19, 2010 If you know you function is returning an array, just like you have indicated, then you can decide how to handle it, either by picking elements you need, or by unpacking it with implode, or by using foreach to do something with each element. Basically it depends what you have put into the array, and what you want to do with each element, and of course you will know what is in there, since it is your function Link to comment https://forums.phpfreaks.com/topic/192609-building-a-function-with-multiple-outputs/#findComment-1014733 Share on other sites More sharing options...
monkeytooth Posted February 19, 2010 Author Share Posted February 19, 2010 well I guess where I am stuck with this notion cause I am looking. Is once the function is called via php in another section of the site how would I output that array array($function()); Link to comment https://forums.phpfreaks.com/topic/192609-building-a-function-with-multiple-outputs/#findComment-1014739 Share on other sites More sharing options...
jl5501 Posted February 19, 2010 Share Posted February 19, 2010 ok so you function returns an array. So you call it like this $myreturnedarray = myfunctionthatreturnsanarray(); So now you have your array and can extract it, pass it to another function or whatever you need. Link to comment https://forums.phpfreaks.com/topic/192609-building-a-function-with-multiple-outputs/#findComment-1014742 Share on other sites More sharing options...
monkeytooth Posted February 19, 2010 Author Share Posted February 19, 2010 Then i just explode it or call it out in a foreach or something. Ok, that makes more sense to me now, In my head I was dragging it out in ways that didn't make a lick of sense nor would they work. Thanks for your help and your patients Link to comment https://forums.phpfreaks.com/topic/192609-building-a-function-with-multiple-outputs/#findComment-1014745 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.