如何制作PHP计数器?

时间:2022-06-28 01:23:28

I know I have done this in Javascript once, but how can I make it in PHP? Basically I want to do this:

我知道我用Javascript做过一次,但是我怎么用PHP做呢?基本上我想这样做:

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

But it didn't work when I tried. How would I go about doing this?

但当我试的时候,它不起作用。我该怎么做呢?

Thank you :)

谢谢你:)

EDIT: This is so I can do:

编辑:这就是我能做的:

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

EDIT:

编辑:

This is all in a "while()" so that I can count how many times it goes on, because LIMIT will not work for the query I'm currently doing.

这都是在“while()”中进行的,这样我就可以计算它执行了多少次,因为我正在执行的查询无法使用LIMIT。

3 个解决方案

#1


10  

why the extra if into the while? i would do this:

为什么额外的如果进入时间?我想这样做:

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

echo $counter;

#2


4  

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

要对给定的值进行递增,请将递增操作符++附加到整数变量中,并将其直接放在while循环中,而不用使用条件表达式来检查变量是否已设置。

$counter = 1;

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

If your counter is used to determine how many times your code is to be executed then you can place the condtion within your while() expression:

如果使用计数器来确定要执行多少次代码,那么可以将condtion放在while()表达式中:

while($counter < 10){
    echo "plus one to $counter";
    $counter++;
}

echo("Counter is $counter!");  // Outputs: Counter is 10!

#3


2  

You're going to have to learn the basics of how PHP outputs to the screen and the other controls along with it.

您将不得不学习PHP如何输出到屏幕以及其他控件的基本知识。

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

Something along those lines will work for you.

沿着这些线的东西会对你有用。

PHP is pretty flexible with what you throw at it. Just remember, statements need a semicolon at the end, and if you want to output to the screen, (in the beginning) you'll be relying on echo statements.

PHP非常灵活。只需记住,语句末尾需要一个分号,如果您想要输出到屏幕,(在开始)您将依赖于echo语句。

Also, when dealing with echo statements, notice the difference between single quotes and double quotes. Double quotes will process any contained variables:

同样,在处理echo语句时,请注意单引号和双引号之间的区别。双引号将处理任何包含的变量:

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

#1


10  

why the extra if into the while? i would do this:

为什么额外的如果进入时间?我想这样做:

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

echo $counter;

#2


4  

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

要对给定的值进行递增,请将递增操作符++附加到整数变量中,并将其直接放在while循环中,而不用使用条件表达式来检查变量是否已设置。

$counter = 1;

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

If your counter is used to determine how many times your code is to be executed then you can place the condtion within your while() expression:

如果使用计数器来确定要执行多少次代码,那么可以将condtion放在while()表达式中:

while($counter < 10){
    echo "plus one to $counter";
    $counter++;
}

echo("Counter is $counter!");  // Outputs: Counter is 10!

#3


2  

You're going to have to learn the basics of how PHP outputs to the screen and the other controls along with it.

您将不得不学习PHP如何输出到屏幕以及其他控件的基本知识。

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

Something along those lines will work for you.

沿着这些线的东西会对你有用。

PHP is pretty flexible with what you throw at it. Just remember, statements need a semicolon at the end, and if you want to output to the screen, (in the beginning) you'll be relying on echo statements.

PHP非常灵活。只需记住,语句末尾需要一个分号,如果您想要输出到屏幕,(在开始)您将依赖于echo语句。

Also, when dealing with echo statements, notice the difference between single quotes and double quotes. Double quotes will process any contained variables:

同样,在处理echo语句时,请注意单引号和双引号之间的区别。双引号将处理任何包含的变量:

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