fesan Posted November 24, 2012 Share Posted November 24, 2012 Happy Saturday. Can someone explain why this code returns nothing? function test(){ $test = "test"; } test(); echo $test;} Quote Link to comment https://forums.phpfreaks.com/topic/271104-functions/ Share on other sites More sharing options...
Barand Posted November 24, 2012 Share Posted November 24, 2012 (edited) because variables are local to functions. You need function test(){ $test = "test"; return $test; } echo $test(); Edited November 24, 2012 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/271104-functions/#findComment-1394744 Share on other sites More sharing options...
fesan Posted November 24, 2012 Author Share Posted November 24, 2012 (edited) ahh... Thanks. And if there are allot of variables i need to get out of the function? Do i have to return them all? Edited November 24, 2012 by fesan Quote Link to comment https://forums.phpfreaks.com/topic/271104-functions/#findComment-1394746 Share on other sites More sharing options...
Pikachu2000 Posted November 24, 2012 Share Posted November 24, 2012 Multiple values are usually easiest to return in an array. Quote Link to comment https://forums.phpfreaks.com/topic/271104-functions/#findComment-1394748 Share on other sites More sharing options...
50r Posted November 24, 2012 Share Posted November 24, 2012 function test(){ $test = "test"; $foo ="me"; $boo ="we"; return array($test,$foo,$boo); } $array = test(); then go ahead and use the valuable from the array. Quote Link to comment https://forums.phpfreaks.com/topic/271104-functions/#findComment-1394763 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.