Adam W Posted June 7, 2007 Share Posted June 7, 2007 Is it possible to return more than 1 variable from a function and if so how do I do it. Thanks Link to comment https://forums.phpfreaks.com/topic/54590-2-variables-from-a-function/ Share on other sites More sharing options...
taith Posted June 7, 2007 Share Posted June 7, 2007 no... however... if you declare a variable a global... you can use it outside... function test(){ global $test; $test='snuffalufagus'; return 'test'; } echo test(); echo $test; Link to comment https://forums.phpfreaks.com/topic/54590-2-variables-from-a-function/#findComment-269925 Share on other sites More sharing options...
Adam W Posted June 7, 2007 Author Share Posted June 7, 2007 Thanks - that is a pity, I know that you can do this in python, I'll just do 2 calls to seperate functions. Link to comment https://forums.phpfreaks.com/topic/54590-2-variables-from-a-function/#findComment-269929 Share on other sites More sharing options...
taith Posted June 7, 2007 Share Posted June 7, 2007 ya... cuz as soon as you return a value... function ends then and there... Link to comment https://forums.phpfreaks.com/topic/54590-2-variables-from-a-function/#findComment-269939 Share on other sites More sharing options...
trq Posted June 7, 2007 Share Posted June 7, 2007 You could return an array. <?php function foo() { $a = 'foo'; $b = 'bar'; return array($a,$b); } $foo = foo(); echo $foo[0].$foo[1]; ?> Link to comment https://forums.phpfreaks.com/topic/54590-2-variables-from-a-function/#findComment-269944 Share on other sites More sharing options...
per1os Posted June 7, 2007 Share Posted June 7, 2007 You could also return an object if wanted... <?php function foo() { $return->a = 'foo'; $return->b = 'bar'; return $return; } $foo = foo(); echo $foo->a.$foo->b; ?> Link to comment https://forums.phpfreaks.com/topic/54590-2-variables-from-a-function/#findComment-269988 Share on other sites More sharing options...
trq Posted June 7, 2007 Share Posted June 7, 2007 You could also return an object if wanted... Not like that you couldn't. You need to define the class first and its alot of overhead for no gain really. Link to comment https://forums.phpfreaks.com/topic/54590-2-variables-from-a-function/#findComment-269992 Share on other sites More sharing options...
per1os Posted June 7, 2007 Share Posted June 7, 2007 Not like that you couldn't. You need to define the class first and its alot of overhead for no gain really. I should note that it does work with PHP 4.x, just tested. I am unsure with PHP 5, so you could be right given the right version of PHP. But that does work with PHP 4. Whether or not it is considered "correct" or "good" programming I guess is something else... Link to comment https://forums.phpfreaks.com/topic/54590-2-variables-from-a-function/#findComment-269994 Share on other sites More sharing options...
trq Posted June 7, 2007 Share Posted June 7, 2007 Sorry frost, I take that back. Learn something everyday. Just tested it on 5.2.2 and yeah.. its works. I could have swarn theat would have generated a 'method called on a non object' or whatever error that is. Link to comment https://forums.phpfreaks.com/topic/54590-2-variables-from-a-function/#findComment-269997 Share on other sites More sharing options...
per1os Posted June 7, 2007 Share Posted June 7, 2007 Sorry frost, I take that back. Learn something everyday. Just tested it on 5.2.2 and yeah.. its works. I could have swarn theat would have generated a 'method called on a non object' or whatever error that is. I used to think that too, until one day I tried it out. My life has not been the same since ^.- =) But as I stated, unsure if it is good programming or not, as PHP may decide that it should throw an error one day. But as far as I know it works and there is no less efficiency or anything and it works pretty nicely Link to comment https://forums.phpfreaks.com/topic/54590-2-variables-from-a-function/#findComment-269999 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.