0

I have a function that returns a multidimensional array, but I also need to return a single value. I am currently using the single value via the global keyword, so I can modify it inside the function.

But I'm wondering if there is another/better way to return 2 values from a function?

Current (pseudo) code:

global $iNumber;
$arrResult;
{do some calculations and queries}

$iNumber = 73;
return $arrResult; 

The function that called this function can use the array of arrays, and also the global variable which has been updated to 73.

But is there another/better way to combine or pass these two different values?

Rizier123
  • 58,877
  • 16
  • 101
  • 156
SammyG
  • 25
  • 5
  • 1
    You can pass it by reference and modify it that way, or you return an array containing the result array and the number. After that you can extract both values from the returned array. – Rizier123 Aug 31 '16 at 23:11
  • @Rizier123 Someone else posted the second method as an answer, why don't you post an answer with the reference parameter? – Barmar Aug 31 '16 at 23:25
  • @Barmar I mainly moderate on SO and with seeing many bad and "give me teh codez" questions I evolved an answer box allergy over time :P But you're welcome to post it as an answer if your want. – Rizier123 Aug 31 '16 at 23:31
  • @Rizier123 Can you show me the code for passing by reference, please? Just kidding ;-) I'll look it up. thank you for the answer. And thank you to whomever edited my question to showcase the code. – SammyG Sep 01 '16 at 02:12
  • 1
    @SammyG See the manual: http://php.net/manual/en/language.references.pass.php if you get stuck, just say it. Also see: http://stackoverflow.com/help/formatting how you can format your question nicely. Also note that "thanks" is considered noise. Just upvote useful answers. – Rizier123 Sep 01 '16 at 02:15
  • I didn't phrase my question correctly. Is it OK to pass the value as a global variable like I'm doing or should I create and array/object with both results to return with? – SammyG Sep 01 '16 at 02:16
  • 1
    @SammyG I wouldn't recommend to use global. See: http://stackoverflow.com/a/16959577/3933332 at the bottom under "the wrong way: global". So as you like it you can either do it by reference or by returning an array. – Rizier123 Sep 01 '16 at 02:19
  • 1
    @SammyG Also maybe you want to read this: http://meta.stackoverflow.com/a/261593/3933332 not to scare you off :), just that you have a good SO experience in the future and know that SO is quite different how to ask correctly. – Rizier123 Sep 01 '16 at 02:25

2 Answers2

2

You can do it in two ways: to return an array or an object.

Array solution:

function calculate(...) {
   //do some stuff
   return ['result' => $arrResult, 'iNumber' => $iNumber];
}

Object solution:

function calculate(...) {
   //do some stuff
   $object = new stdClass();
   $object->result = $arrResult;
   $object->iNumber = $iNumber;
   return $object;
}

stdClass is just an example and you can create your own class for this purpose.

Andrej
  • 7,474
  • 1
  • 19
  • 21
  • Great answer! I didn't know about stdClass – Bolli Aug 31 '16 at 23:24
  • I didn't think of making another array to put them in. I like that idea; although each answer given discusses the object solution, so there must be something to that as well. Thanks. – SammyG Sep 01 '16 at 02:11
0

You can make a class and use a class variable.

Something like this:

class Test
{
  public $iNumber;

  function __construct()
  {
    # code...
  }

  public function yourCal()
  {
    // you calculations
    $this->setNumber(73);

    return $arrayOfArrays;
  }

  public function getNumber()
  {
    return $this->iNumber;
  }

  public function setNumber($var)
  {
    $this->iNumber = $var;
  }
}

// Use the class in another php file
include 'Test.php';
$test = new Test();

// do calculations
$arrayOfArrays = $test->yourCal();
// get the number
$iNumber =  $test->getNumber();

echo $iNumber; // returns 73
print_r($arrayOfArrays); // print your array
Bolli
  • 4,974
  • 6
  • 31
  • 47