How can I create a PHP counter?

I know that I did this in Javascript once, but how can I do this in PHP? Basically I want to do this:

if (empty($counter)){ $counter = 1; }else{ "plus one to $counter" ($counter++?) } 

But that did not work when I tried. How can I do it?

Thanks:)

EDIT: This can be done like this:

 if ($counter == 10){ echo("Counter is 10!"); } 

EDIT:

This is all in "while ()", so I can calculate how many times this happens, because LIMIT will not work for the query I'm doing now.

+4
source share
3 answers

why extra if at that time? I would do this:

 $counter = 0; while(...) { (...) $counter++; } echo $counter; 
+9
source

You will need to learn the basics of how PHP is displayed and other controls with it.

 if (empty($counter)){ $counter = 1; }else{ echo 'plus one to $counter'; $counter++; } 

Something along these lines will work for you.

PHP is pretty flexible with what you throw at it. Just remember that statements must have a semicolon at the end, and if you want to display (at the beginning), you will rely on echo statements.

Also, when working with echo operators, pay attention to the difference between single quotes and double quotes. Double quotes will process any contained variables:

 $counter = 3; echo "plus one to $counter"; // output: plus one to 3 echo 'plus one to $counter'; // output: plus one to $counter 
+2
source

To increase the given value, attach the increment ++ operator to your integer variable and place it inside the while loop directly, without using a conditional expression, to check if the variable is set or not.

 $counter = 1; while(...){ echo "plus one to $counter"; $counter++; } 

If your counter is used to determine how many times your code should be executed, you can put condtion in your while() expression:

 while($counter < 10){ echo "plus one to $counter"; $counter++; } echo("Counter is $counter!"); // Outputs: Counter is 10! 
+2
source

All Articles