spfoonnewb Posted April 8, 2007 Share Posted April 8, 2007 How can I make this recursive.. so that I don't have to put one for each? So say I want to have 10 $n[0]'s be evaluated.. then do that.. or if I want 68.. Instead of doing it like this for 5..? if($x + $n[0] + $n[0] + $n[0] + $n[0] + $n[0] == $number) { echo "$x + $n[0] + $n[0] + $n[0] + $n[0] + $n[0]"; } Link to comment https://forums.phpfreaks.com/topic/46095-for-loop-repeat-variable/ Share on other sites More sharing options...
per1os Posted April 8, 2007 Share Posted April 8, 2007 function createDisplay($n, $num) { global $number; $num--; if ($num == 0) { echo "$x + $n[0] + $n[0] + $n[0] + $n[0] + $n[0]"; return; }elseif($x + $n[0] + $n[0] + $n[0] + $n[0] + $n[0] == $number) { createDisplay($n, $num); echo "$x + $n[0] + $n[0] + $n[0] + $n[0] + $n[0]"; } return; } That may not work, but you should get the idea from it. Link to comment https://forums.phpfreaks.com/topic/46095-for-loop-repeat-variable/#findComment-223979 Share on other sites More sharing options...
spfoonnewb Posted April 8, 2007 Author Share Posted April 8, 2007 Ok, that seems to work for printing them recursively, but I need the returned value to be evaluated/used/executed in an if statement like it is in my example. All it does it print it out.. I end up with a bunch of +, +, +... Link to comment https://forums.phpfreaks.com/topic/46095-for-loop-repeat-variable/#findComment-223980 Share on other sites More sharing options...
per1os Posted April 8, 2007 Share Posted April 8, 2007 Sorry you could not be more specific, perhaps you should read up on recursion and try and figure it out on your own. All I was providing is a basis to work from, hopefully you can do some research and figure it out on your own. Hopefully. Link to comment https://forums.phpfreaks.com/topic/46095-for-loop-repeat-variable/#findComment-223981 Share on other sites More sharing options...
spfoonnewb Posted April 8, 2007 Author Share Posted April 8, 2007 I've been trying to figure it out all day.. Maybe I can explain it better.. So right now I have it set at 5.. (There are 5 $n[0] in the if statement). if($x + $n[0] + $n[0] + $n[0] + $n[0] + $n[0] == $number) { //Dont worry about this.. } If I want it at 10 I add 5 more " + $n[0] " .. All I am trying to figure out how to do is make it so that I can say I want 5 $n[0] and the script run with 5.. but without literally manually inserting 5 of them like I did above. They need to execute like they are in the example above, and not evaluate " $n[0] until it is in the if statement as if I put them there like they are in the example. I just want them there automatically based on a specified number. It is kind of hard to explain... Link to comment https://forums.phpfreaks.com/topic/46095-for-loop-repeat-variable/#findComment-223983 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.