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] Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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? Quote Link to comment 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 ^_^ Quote Link to comment 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.