Jump to content

For loop - repeat variable


spfoonnewb

Recommended Posts

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

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.

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...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.