Jump to content

can a PHP function return more than 1 value ??


truegilly

Recommended Posts

Hi there,

im a total newbie with php and was wondering if a php function can return more than one value.

the function below will return a variable that contains the name of a colour. in the function i have queried my database and stored values in the variables [color=red]$Target [/color]and [color=red]$Actual[/color].

it would be nice to be able to return more values than just the return, and was wondering if this is possible.

below is my function

[code]function consolidate(){

//perform query
$query = 'SELECT Target_No_Of_VC10, Actual_No_Of_VC10 FROM vc10_node';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

// get a single row from the database
$row = mysql_fetch_row($result);

$Target = $row[0];
$Actual = $row[1];
$colour = null;

// simple function

if ($Target == $Actual)
{
$colour = "green";
return $colour;
}
else if ($Actual < $Target)
{
$colour = "red";
return $colour;
}
else {
$Actual > $Target;
$colour = "blue";
return $colour;
}
}[/code]

what i would like to do is return the value of the the variables $target and $actual without having to write a new function.

any help would be appreciated.

truegilly
you can, return an array, and use that data

for example

[code=php:0]
function MyFunction(){
$MyName = "Jamie";
$MyCountry = "UK";

return array($MyName, $MyCountry);
}

list($MyName, $MyCountry) = MyFunction();

echo $MyName."<br />\n"
.$MyCountry."<br />\n";

[/code]
Hi Guys,

thanks for the reply's...

had a go and it is returning values but i am getting letters coming back rather than numbers, which is what they are set to in my database.

the target is set to 5 ($Target)
and the actual is set to 6 ($Actual)

any ideas why this is happening ??

any help is most appreciated !!  ???
now you're returning an array, make sure you're accessing the return result's elements, not the whole thing.

[code]
<?php
$ret = consolidate();

// correct
echo $ret['actual'];
echo $ret['target'];

// not correct
echo $ret;
?>
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.