jwk811 Posted November 15, 2006 Share Posted November 15, 2006 can you return more than one variable in a function like this?:return $a, $b, $c, $d;or will you have to make an array? if its an array could someone please show me how to do this?thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/27378-functions-return-more-than-one-vairable/ Share on other sites More sharing options...
obsidian Posted November 15, 2006 Share Posted November 15, 2006 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]<?phpfunction 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 Quote Link to comment https://forums.phpfreaks.com/topic/27378-functions-return-more-than-one-vairable/#findComment-125194 Share on other sites More sharing options...
jwk811 Posted November 15, 2006 Author Share Posted November 15, 2006 great thanks! Quote Link to comment https://forums.phpfreaks.com/topic/27378-functions-return-more-than-one-vairable/#findComment-125197 Share on other sites More sharing options...
blear Posted November 15, 2006 Share Posted November 15, 2006 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:returnValueAreturnValueBreturnValueCreturnValueDA little more complicated, but arrays always seem to come up wit new and exciting ways to upset me. Quote Link to comment https://forums.phpfreaks.com/topic/27378-functions-return-more-than-one-vairable/#findComment-125199 Share on other sites More sharing options...
jwk811 Posted November 15, 2006 Author Share Posted November 15, 2006 Warning: Missing argument 1 for displaysettings() why would this come up? and this is with the arraythe error is focused on the first line of my function Quote Link to comment https://forums.phpfreaks.com/topic/27378-functions-return-more-than-one-vairable/#findComment-125203 Share on other sites More sharing options...
obsidian Posted November 15, 2006 Share Posted November 15, 2006 Can you show your code? It's a little easier to debug that way ;) Quote Link to comment https://forums.phpfreaks.com/topic/27378-functions-return-more-than-one-vairable/#findComment-125207 Share on other sites More sharing options...
jwk811 Posted November 15, 2006 Author Share Posted November 15, 2006 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> </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> </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> </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 Link to comment https://forums.phpfreaks.com/topic/27378-functions-return-more-than-one-vairable/#findComment-125209 Share on other sites More sharing options...
trq Posted November 15, 2006 Share Posted November 15, 2006 Without seeing how you call the function its a little hard to tell what your doing. But to define function argumants as being optional you can use something like this....[code=php:0]function displaySettings($action=""){[/code] Quote Link to comment https://forums.phpfreaks.com/topic/27378-functions-return-more-than-one-vairable/#findComment-125213 Share on other sites More sharing options...
jwk811 Posted November 15, 2006 Author Share Posted November 15, 2006 that worked! thanks Quote Link to comment https://forums.phpfreaks.com/topic/27378-functions-return-more-than-one-vairable/#findComment-125225 Share on other sites More sharing options...
jwk811 Posted November 15, 2006 Author Share Posted November 15, 2006 actually its making $action = '' Quote Link to comment https://forums.phpfreaks.com/topic/27378-functions-return-more-than-one-vairable/#findComment-125229 Share on other sites More sharing options...
obsidian Posted November 15, 2006 Share Posted November 15, 2006 [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. Quote Link to comment https://forums.phpfreaks.com/topic/27378-functions-return-more-than-one-vairable/#findComment-125241 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.