Miguel
Miguel

Reputation: 1897

PHP count number of loops for

This is just by curiusity, how can i count the number of times a given cycle works. for instance:

    for($x=1; $x <=5; $x++) {

    for ($i=1; $i <= 5; $i++) {

        for($y=1; $y<=5; $y++) {

            for($j=1; $j<=5; $j++) {

                for($z=1; $z<=5; $z++) {

                    echo($x." - ".$i." - ".$y." - ".$j." - ".$z."<br>");

                }
            }
        }
    }
}

And the display is something like this:

1 - 1 - 1 - 1 - 1
1 - 1 - 1 - 1 - 2
1 - 1 - 1 - 1 - 3
1 - 1 - 1 - 1 - 4
1 - 1 - 1 - 1 - 5
1 - 1 - 1 - 2 - 1
...

All works fine, and i know by math the $z cycle is changed 3125 times, but i'm sure there is a way to count this. Any tips? Thanks

Upvotes: 0

Views: 67

Answers (1)

Tobias Golbs
Tobias Golbs

Reputation: 4616

$count = 0;
for($x=1; $x <=5; $x++) {
    for ($i=1; $i <= 5; $i++) {
        for($y=1; $y<=5; $y++) {
            for($j=1; $j<=5; $j++) {
                for($z=1; $z<=5; $z++) {
                    echo($x." - ".$i." - ".$y." - ".$j." - ".$z."<br>");
                    $count++;
                }
            }
        }
    }
}
echo $count;

Upvotes: 4

Related Questions