Jump to content

functions.. return more than one vairable?


jwk811

Recommended Posts

A return value in a function can only return [b]one[/b] object, so your question about the array is the solution. Here is an easy way to do what you're after:
[code]
<?php
function myVars() {
  $a = 1;
  $b = 2;
  $c = 3;
  return array($a, $b, $c);
}

$vars = myVars();

// either do this:
echo "$vars[0], $vars[1], $vars[2]";

// or this:
list($a, $b, $c) = $vars;
echo "$a, $b, $c";
?>
[/code]

Hope this helps
You can also create an object wrapper with the data in it.
[code] class wrapper {
$a='valueOfA';
$b='valueOfB';
$c='valueOfC';
$d='valueOfD';
}


function yourFunction {
$returnData = new wrapper;
$returnData->a = 'returnValueA';
$returnData->b = 'returnValueB';
$returnData->c = 'returnValueC';
$returnData->d = 'returnValueD';
return $returnData;
}
[/code]

Then, to use the function, you would need another instance of wrapper to take the wrapper object.

[code]$returnTaker = new wrapper;
$returnTaker = yourFunction();
echo $returnTaker->a;
echo $returnTaker->b;
echo $returnTaker->c;
echo $returnTaker->d;
[/code]

The echo statements would then print to screen:

returnValueAreturnValueBreturnValueCreturnValueD


A little more complicated, but arrays always seem to come up wit new and exciting ways to upset me.
here is the function i am using.. and by the way the page and all the contents show up but up at the top there is that warning... would it have anything to do with no $action being present? because as you can see im checking to see if there is a $action available and if not then show the defaults.
[code]function displaySettings($action){

$default_top = '<table width="541">
<tr>
<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td>
<br>
<form method="post" action="login.php">
<table width="200" height="100" border="5">
<tr>
<td style="font-size: 18px"><b>Username</b></td>
<td><input type="text" name="username" size="13"></td>
</tr>
<tr>
<td style="font-size: 18px"><b>Password</b></td>
<td><input type="password" name="password" size="15"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Login"></td>
</tr>
</table>
<br>
</td>
<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td>
<table width="200" height="100" border="5">
<tr>
<td align="center" valign="middle">
<b style="font-size: 18px">REGISTER</b>
</td>
</tr>
<tr>
<td valign="middle" align="center">
<br><form method="link" action="index.php?action=register">
<input type="submit" value="Click Here">
</form>
</table>
</td>
<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
</tr>
</table>';

$default_title = "Welcome";

$default_content = "Default Content Here";

if(isset($action)){
    if($action == 'logout'){
$display_title = $logout_title;
$display_content = $logout_content;
    }
    else if($action == 'nolog'){
$display_title = $nolog_title;
$display_content = $nolog_content;
    }
    else {
$display_top = $default_top;
$display_title = $default_title;
$display_content = $default_content;
    }
    return array($display_title, $display_content, $display_top);

}
else {
    $display_top = $default_top;
    $display_title = $default_title;
    $display_content = $default_content;
    return array($display_title, $display_content, $display_top);

}
}
?>[/code]
[quote author=jwk811 link=topic=115112.msg468622#msg468622 date=1163630678]
actually its making $action = ''
[/quote]

Right. What thorpe is suggesting sets a default value to $action in case you don't pass in a value. The way your function is set up, it is expecting you to pass in [b]something[/b] for an $action, but if you don't, it will default to an empty string.

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.