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
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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 !!  ???
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.