phppup Posted October 7, 2022 Share Posted October 7, 2022 how can I retrieve the single variable that I want from the function? A similar example for what I'm attempting to accomplish $seq=""; function doThis($num=4){ for($i=0; $i<$num; $i++){ echo $i."<br>"; $seq .= $i; } echo $seq; } doThis(); echo $seq; //get variable here How can I access the variable that is created inside the function, and use it on the outside? Quote Link to comment https://forums.phpfreaks.com/topic/315409-retrieve%C2%A0single%C2%A0variable%C2%A0from-a%C2%A0function/ Share on other sites More sharing options...
Barand Posted October 7, 2022 Share Posted October 7, 2022 Return the value from the function https://www.php.net/manual/en/functions.returning-values.php Quote Link to comment https://forums.phpfreaks.com/topic/315409-retrieve%C2%A0single%C2%A0variable%C2%A0from-a%C2%A0function/#findComment-1601403 Share on other sites More sharing options...
benanamen Posted October 7, 2022 Share Posted October 7, 2022 <?php function doThis($num = 4) { for ($i = 0; $i < $num; $i++) { $seq[] = $i; } return $seq; } $arr = doThis(); echo $arr[1]; echo $arr[2]; echo $arr[3]; Quote Link to comment https://forums.phpfreaks.com/topic/315409-retrieve%C2%A0single%C2%A0variable%C2%A0from-a%C2%A0function/#findComment-1601404 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.