Web Hosting Talk







View Full Version : Multiple variables returned by a function


blazenet
12-20-2002, 02:11 PM
Hello,

Is it possible to do something like this in PHP?


<?
function test () {
$foo = "hello";
$bar = "bye";
return $foo, $bar;
}

$a, $b = test();
echo "a = $a, b = $b";
?>


This gives parse errors, but maybe there is another way in PHP to handle this? Anyone has any ideas why/why not this should be possible?

stephenM
12-20-2002, 02:48 PM
Just a thought, maybe:

return($foo, $bar);

or

return($foo $bar);

Try something like this ;)

inverus
12-20-2002, 04:39 PM
This should work:


<?php
function test()
{
$foo = "hello";
$bar = "bye";
return array($foo, $bar);
}

list($a, $b) = test();
print "a = $a, b = $b";
?>

GanonOfEvil
12-20-2002, 04:53 PM
return() can only return one item, but that item may be an array. inverus' example will work.

More info: http://www.php.net/manual/en/functions.returning-values.php

Paint
12-20-2002, 05:51 PM
I don't know much of php... but i know in C++ there is a pass thru code.... like instead of a return you would have function test(int &var1, int &var2) The & sign tells the computer to take in two variables and return the new value in the main function. hope it helps... anyone tell me if i am wrong

Rich2k
12-20-2002, 07:43 PM
Is it something you can use a class for as the variables would be global to the class... still you can only return one variable but you could actually have your output as a function within that class and thus make use of more than one variable.

Saeven
12-20-2002, 10:27 PM
You can do several things, serialize it, store it into a session variable, or return an array.

Serializing would involve turning the array into a string, this is old school, but is still done quite often, more so when arrays need to be stored into mySQL.

You could do it as follows:


function test(){
$string = 'hello'.':'.'bye';
return $string;
}

and in the calling function perform this:


$array = explode( ':', test() );

foreach( $array as $val ){
echo $val.'<br>';
}

iWebbers.com
12-21-2002, 06:21 AM
You can try to pass the variables by reference.


<?php
function test(&$foo, &$bar)
{
$foo = "hello";
$bar = "bye";
}
test($foo,$bar);
print "foo = $foo, bar = $bar";
?>

dreamcost
12-22-2002, 11:28 AM
You could also use an array:


<?
function test () {
$arr[foo] = "hello";
$arr[bar] = "bye";
return $arr;
}

$arr = test();
echo "a = $arr[foo], b = $arr[bar]";
?>

dreamrae.com
12-22-2002, 01:01 PM
i was just gonna say, use an array. personally, i think arrays are the best think about most programing languages, all that data you can store. :D

dreamrae.com
12-22-2002, 01:07 PM
this function is something i made awhile ago for a news site. the first function returns an array,
the 2nd shows you how to read it. the bet part is, it sahows you how to store an array within an array :D




function get_topic_id($post_id){
include("../includes/include.inc");
$sql=mysql_connect($dbhost,$dbuser,$dbpasswd);
mysql_select_db($dbname2);
$retid=mysql_db_query($dbname2,"SELECT * FROM phpbb_posts",$sql);
$r=mysql_numrows($retid);
$i=0;
while($i!=$r){
$tmp_post_id=@mysql_result($retid,$i,"post_id");
$topic_id_num=@mysql_result($retid,$i,"topic_id");
$post_time=@mysql_result($retid,$i,"post_time");
$poster_id=@mysql_result($retid,$i,"poster_id");
if ($tmp_post_id==$post_id){
mysql_close($sql);
$total[0]=$topic_id_num;
$total[1]=getdate($post_time);
$total[2]=get_user_name($poster_id);
$total[3]=$poster_id;
return $total;
}
$i++;
}
@mysql_close($sql);
return 0;
}





$tmp_total=get_topic_id($post_id);
$topic_id=$tmp_total[0];
$tmp_day=$tmp_total[1];
$day=$tmp_day['mon'];
$day.="/";
$day.=$tmp_day['mday'];
$day.="/";
$day.=$tmp_day['year'];
$attime=$tmp_day['hours'];
if (date('I')==0){
$attime--;
}
if ($attime>12){
$attime=$attime-12;
}
if ($tmp_day['hours']>=12){
$m="pm";
}else{
$m="am";
}
$attime.=":";
if (strlen($tmp_day['minutes'])==1){
$attime.="0";
$attime.=$tmp_day['minutes'];
}else{
$attime.=$tmp_day['minutes'];
}
$sql1=mysql_connect($dbhost,$dbuser,$dbpasswd);
echo "<li><font size=\"1\"><a target=\"blank\"
href=\"http://forums.********.net/viewtopic.php?t=$topic_id\">
<b>$post_subject</b></a><b> by</b></font> <a target=\"blank\"
href=\"http://forums.********.net/profile.php?
mode=viewprofile&u=$tmp_total[3]\"><font color=\"#808080\"><b>
$tmp_total[2]</b></font></a><font color=\"#FFFFFF\"
size=\"1\"><b> on $day @ $attime$m</b></font></li>";
$c++;
}
if ($c>=$count){
break;
}
$r--;
}
echo "</ul>";
mysql_close($sql1);
}