0

How do I create a counter from two for-loops in PHP?

for ($x=9; $x<=12; $x++) {
  for ($y=1; $y<=31; $y++) {
    echo $y.'<br>';
    ////// Now it runs well, but I want to add $y into number 
    ///// I know that this loop total run 124 time  so i want 124 
  }
}
1
  • yes last result will be the thing that you want. Commented Aug 20, 2014 at 21:05

3 Answers 3

1

Try this:

$z = 0;
for ($x=9; $x<=12; $x++) {
  for ($y=1; $y<=31; $y++) {
    echo $y.'<br>';
    $z++;
  }
}

echo $z;

Add another variable and just count it up.

Sign up to request clarification or add additional context in comments.

2 Comments

sidestep - ++$z is better in big loops
works fine i Just put $z; after $z++; now work as i want
1

Unless I'm misunderstanding what you want:

$counter = 0;//declare your counter here
for ($x=9; $x<=12; $x++) {
  for ($y=1; $y<=31; $y++) {
    echo $y.'<br/>';//The '$y' variable only exists inside this 'scope'. That is why counter must be declared BEFORE the for-loop.
    $counter++;//Add one for each time it goes through this loop.
  }
}
echo $counter;

I think the piece of understanding that you need is about 'scope'. Loosely, anything you see with brackets ({ }) is probably a new scope, or context. Scopes can generally see the scopes that declared them, but cannot see into scopes they declare. Thus, in the above example, the largest scope is where $counter is declared, but it cannot see the $y variable because it is declared in an internal scope.

//This is the 'outer scope'
$counter = 0;//Any scopes internal to this can see this variable.
for ($x=9; $x<=12; $x++) {//This declares a new scope internal to the outer scope. It can see $counter but not $y.
  for ($y=1; $y<=31; $y++) {//This declares a new scope internal to both other scopes. It can see $x and $counter.
    echo $y.'<br/>';
    $counter++;
  }
  //Note that here we can 'see' $counter and $x, but not $y, even though $y has been declared.
  //This is because when we leave the internal for loop it's 'scope' and any variables associated
  //are discarded and no longer accessible.
}
echo $counter;//At this point only the counter variable is still around, because it was declared by this scope.

6 Comments

While it is strictly true in php you can continue to access variables, generally you cannot rely on this, and it is, I would argue, extremely bad practice if you do.
"Generally" speaking, you might avoid it either explicitly by convention or implicitly by paradigm but only really for reasons of clarity - the interpreter is consistent in it's lack of an isolated scope within loops. The likely hood of that ever changing is extremely low for issues of backwards compatibility - people can and do rely on the ability to declare variables as and when they're needed within a given context - it's actually quite prevalent amongst weak-typed languages. There's no real basis for it being "bad practice", that's entirely subjective. Your post is just incorrect.
You saying that utilizing a variable declared in the clause of a for-loop declaration after the end of that loop is good practice? And that because some weakly typed languages allow it (even though the other half of programming languages do not allow it), it is appropriate to teach an obvious newcomer to expect that behavior?
Note that I grant php does not work that way, and that it is unlikely to change, but within the context of for loops as a language-independent construct, this is the correct understanding, as evidenced by every answer-poster suggesting the declaration of a counter variable prior to the for loop for this purpose.
Like I said, "for reasons of clarity" you might avoid it but I was being deliberately impartial because obviously it depends on circumstance - of course in a global context it gets messy but it's not inherently "bad"... Arguably there's nothing wrong with exploiting it for the sake of brevity, for example inside a function where it might be clear what is going on and / or you know that the variable will die with the return.
|
0

Just create a counter variable and increment it in the loop:

$counter = 0;
for ($x=9; $x<=12; $x++) {
  for ($y=1; $y<=31; $y++) {
    echo $y.'<br>';
    ////// Now it run good but i want add $y into number 
    ///// I know that this loop total run 124 time  so i want 124 
    $counter++;
  }
}
echo $counter;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.