Hellusius Posted September 3, 2006 Share Posted September 3, 2006 I know this has no real relevance, but I would like to ask why this does not work.I wanted to echo each number from 0 to 20[code]<?phpfunction hello ($amount=25){ for ($i = 0; $i < $amount; $i++) return $i;}?><?phpecho (hello($i));?>[/code] Link to comment https://forums.phpfreaks.com/topic/19550-noob-php-question/ Share on other sites More sharing options...
Joe Haley Posted September 3, 2006 Share Posted September 3, 2006 return halts excecution of a function and returns the specified value, so:[code=php:0]function foo(){ echo 'a'; return false; echo 'b';}foo();[/code]would simpyl echo 'a', as once the function returns, the rest of the code within it is not executed.You could assign the value of $i to a variable, like so:[code=php:0]$string = $string . ' ' . $i;[/code](.= is equivilent to $string = $string . $i)And then return $string. Link to comment https://forums.phpfreaks.com/topic/19550-noob-php-question/#findComment-85043 Share on other sites More sharing options...
Hellusius Posted September 3, 2006 Author Share Posted September 3, 2006 well, I just wanted to echo the number 0 to 20 since the function has to play itself from 0 to 20 right, so I thought, if'd take varb "i" I would be safe Link to comment https://forums.phpfreaks.com/topic/19550-noob-php-question/#findComment-85044 Share on other sites More sharing options...
extrovertive Posted September 3, 2006 Share Posted September 3, 2006 [code=php:0]function hello ($amount=20){ for ($i = 0; $i <= $amount; $i++) echo $i . "<br />\n";}?>$i = 20;hello($i);[/code]Why not just have the function output it to the screen? Link to comment https://forums.phpfreaks.com/topic/19550-noob-php-question/#findComment-85046 Share on other sites More sharing options...
Hellusius Posted September 3, 2006 Author Share Posted September 3, 2006 didn't think of that one, thanks for the header, and [code]function hello ($amount=20){ for ($i = 0; $i <= $amount; $i++) echo $i . "<br />\n";}?><?phphello();?>[/code]is shorter ^_^ Link to comment https://forums.phpfreaks.com/topic/19550-noob-php-question/#findComment-85049 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.