Function returning multiple values? Multiple Arrays?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
superwormy
Forum Commoner
Posts: 67
Joined: Fri Oct 04, 2002 9:25 am
Location: CT

Function returning multiple values? Multiple Arrays?

Post by superwormy »

Can a function return multiple values?

What about multiple arrays, can a function return multiple arrays?

How?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

A function can only return one value, that value can be a number, a string, an array etc. If you want to return multiple values you have to return them in an array whether they are strings or already arrays themselves.

http://www.php.net/manual/en/functions. ... values.php

Mac
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

a common way in C is to pass pointers to the function, i.e. fgets is defined as

Code: Select all

char *fgets( char *string, int n, FILE *stream );
it will write up to n characters in the buffer specified by string.
You may do the same in PHP by passing references to the function, i.e.

Code: Select all

<?php
function division($a, $b, &$remainder)
{
	$remainder = $a % $b;
	return (int)($a / $b);
}
print(division(5, 3, $remainder));
print(' remainder: '.$remainder);
?>
  • but you should
  • consider passing it back in a array
  • clearly mark it if you're using a reference and going to change the parameter's value
p.s.: & replaced by & in php-section :cry:
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

But you don't have pointers in PHP do you?
Anyway I was having trouble with returning an array but here was my problem.

I had made an MySQL Function library. I wanted to return a value fgrom the db using mysql_fetch_array(). The query will return multipl rows. And I tried to put it into an array. Like this:-

Code: Select all

<?php
class class_mysql {
  function execute($sql,$option) {
    switch ($option) {
      case 1:
        $temp = mysql_query($sql);
        $i = 0;
        while(list($result) = mysql_fetch_array($temp)) {
          $arrayї$i] = $result;
        }
    }
    return($result);
  }
}
?>
This didn't work, any ideas?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Try

Code: Select all

$temp = mysql_query($sql);
$num_rows = mysql_num_rows($temp);
for ($i = 0; $i < $num_rows; $i++) {
    $arrayї$i] = mysql_fetch_array($result);
}
and instead of

Code: Select all

return($result);
try

Code: Select all

return $array;
But you don't have pointers in PHP do you?
But you do have references as volka said:
http://www.php.net/manual/en/functions. ... values.php
http://www.php.net/manual/en/language.references.php

Mac
Post Reply