Re: quick php for perl coder question
| From: | Jochem Maas | Date: | Thu, 11 Sep 2008 19:48:36 +0000 |
| Subject: | Re: quick php for perl coder question | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-279918@lists.php.net to get a copy of this message | ||
Thomas Bolioli schreef:
I want to return an array from a function. I have this: return array($found, $username, $email, $nickname); as my code. I have a bug in the code and I am not sure where yet. Should the above statement with this: array($vars);not sure what you meant with the line above, it's valid but does nothing.
$vars = function($type, $abc, $xyz); $found = $vars[0]; $username = $vars[1]; $email = $vars[2]; $nickname = $vars[3]; on the other end work? If so, then the bug is somewhere else.yes this should work assuming you meant this: <?php function foo() { $found = true; $username = 'The Joker'; $email = 'joker@example.com'; $nickname = 'joker'; return array($found, $username, $email, $nickname); } $vars = foo(); $found = $vars[0]; $username = $vars[1]; $email = $vars[2]; $nickname = $vars[3]; ?> although you could do it more perly: <?php list($found, $username, $email, $nickname) = foo(); ?> and maybe consider using an assoc array: <?php function foo() { $found = true; $username = 'The Joker'; $email = 'joker@example.com'; $nickname = 'joker'; return array( 'found' => $found, 'username' => $username, 'email' => $email, 'nickname' => $nickname, ); } $vars = foo(); // using an associative array negates the need to // define the following variables, you can use // $vars['found'] (etc) as it is quite descriptive // in it's own right :-) $found = $vars['found']; $username = $vars['username']; $email = $vars['email']; $nickname = $vars['nickname']; ?>
Thanks in advance, Tom