[英]How to echo only Once on Page in Function in PHP?
Here is my function: 这是我的功能:
curl_setopt( $curl, CURLOPT_PROGRESSFUNCTION, function ( $resource, $download_size, $downloaded_size, $upload_size, $uploaded_size) use ($array)
{
static $previousProgress = 0;
if ( $download_size == 0 ) {
$progress = 0;
} else {
$progress = round( $downloaded_size * 100 / $download_size );
}
As you can see I'm inserting my own variables with use
at the end of the function. 如您所见,我将在函数末尾插入要use
的变量。
What I'm trying to do is simply echo this line ONCE after the else
condition 我想做的就是在else
条件之后仅回显此行一次
echo ' ' . $progress . '%' . ' (' . bytes($downloaded_size) . ' of ' . bytes($download_size) . ') ';
I've tried things such as 我已经尝试过诸如
static $previousProgress = 0;
$count = $previousProgress++;
} else {
if ($count == 1) { echo echo ' ' . $progress . '%' . ' (' . bytes($downloaded_size) . ' of ' . bytes($download_size) . ') '; }
$progress = round( $downloaded_size * 100 / $download_size );
}
But at this point the function has already ran an unknown amount of times (50, or 60 sometimes), before it hits the else
condition. 但是,此时函数在达到else
条件之前已经运行了未知次数(有时50次,有时60次)。
I can of course do this 我当然可以做到
static $previousProgress = 0;
$count = $previousProgress++;
if ($count == 1) { echo ' ' . $progress . '%' . ' (' . bytes($downloaded_size) . ' of ' . bytes($download_size) . ') '; }
And it works to only echo once.. but the issue is CURL
does not have the $downloaded_size
or $download_size
variables set and display as 0 bytes
- therefore it needs to be in the else condition. 而且它只能回显一次。.但是问题是CURL
没有设置$downloaded_size
或$download_size
变量并显示为0 bytes
-因此它必须处于else条件。
I've tried using true
/ false
variables as one would normally do to output something once in a for
or foreach
loop, but since the function is just recursively running until it times out, the function starts over again and this does not work. 我试过使用true
/ false
变量,因为通常会在for
或foreach
循环中一次输出某些内容,但是由于该函数只是递归运行,直到超时,该函数又重新开始,因此不起作用。
As I said, I can insert an array of variables into this function to help me... but I'm not sure how I can do this considering the function starts over and is re-run everytime.. 就像我说的那样,我可以在此函数中插入变量数组以帮助我...但是考虑到该函数重新启动并每次都重新运行,我不确定如何做到这一点。
Tried other random things but I am apparently missing something. 尝试了其他随机的东西,但是我显然丢失了一些东西。 How can I echo the download size only once in the repeating function? 如何在重复功能中仅回显一次下载大小?
A static boolean variable should do what you want. 静态布尔变量应该执行您想要的操作。
static $firstTime = true;
if ($firstTime) {
echo "something";
$firstTime = false;
}
The problem with your code is probably because you were using post-increment. 代码的问题可能是因为您使用的是后增量。 If $previousProgress
is 0
, the line: 如果$previousProgress
为0
,则行:
$count = $previousProgress++;
will set $count
to 0
(the old value) while incrementing $previousProgress
to 1
. 将$count
设置$count
0
(旧值),同时将$previousProgress
递增为1
。 So the if ($count == 1)
condition wasn't true until the second iteration. 因此, if ($count == 1)
条件直到第二次迭代才成立。 If you used ++$previousProgress
it would set them both to 1
. 如果使用++$previousProgress
,则将它们都设置为1
。 That's the difference between pre-increment and post-increment. 那就是前增加和后增加之间的区别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.