...

View Full Version : Can one return more than one variable using the return; structure in PHP?



ibnpaul
05-01-2006, 09:55 PM
Hi all,

I want to return two arrays at the end of a function. Is this possible? If so, is this the correct syntax:


return $array1, $array2;

Thanks a lot.

ibnpaul
05-01-2006, 10:00 PM
Nevermind, that would make problems in assigning the results. Sorry for posting w/o thinking first. :o

I'm just going to assign the second array to an element of the first.

NancyJ
05-01-2006, 10:04 PM
you could return the whole thing as a string.
eg.
return implode(",", $array1)."|".imploode(",",$array1);

then explode it at the other end.
eg.

$returnarrays = explode("|",$returnVal);
$array1 = explode("," $returnarrays[0]);
$array2 = explode(",", $returnarrays[1]);

ibnpaul
05-01-2006, 10:40 PM
Very good idea. Thanks a lot.:thumbsup:

Brandoe85
05-01-2006, 10:52 PM
Another way:


<?php

function rets()
{
$array1 = array('some jazz here', 'some more...');
$array2 = array('another bunch of jibb', 'foobar');
return array($array1, $array2);
}

list($arr1, $arr2) = rets();

echo '<pre>';
print_r($arr1);
print_r($arr2);
echo '</pre>';

?>

GJay
05-01-2006, 11:51 PM
Brandoe's way is a lot nicer, someone else coming along to your and seeing a delimited string being returned might get awfully confused.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum