Php echo only once in loop

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.

What I'm trying to do is simply echo this line ONCE after the else condition

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.

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.

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.

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?

    Table of contents
  • How can i echo only once inside a foreach loop PHP
  • Echo content inside foreach only once
  • Conditions inside foreach loop
  • Echo or Print an Array in PHP
  • Only one item showing outside of foreach loop

How can i echo only once inside a foreach loop PHP

foreach[range[1, $numDays] as $number]:
    $counter++;
    echo $number;

    if[$counter < 8]{
        //echo only once bellow
        echo "
header
"; //echo only once above echo "hello"; //echo only once below echo "
footer
"; //echo only once above } endforeach;
//echo only once bellow
    echo "
header
"; //echo only once above foreach[range[1, $numDays] as $number]: $counter++; echo $number; if[$counter < 8]{ echo "hello"; } endforeach; //echo only once below echo "
footer
"; //echo only once above
foreach[range[1, $numDays] as $number]:
      $counter++;
      echo $number;
      $flag=1;

      if[$counter < 8]{
           //echo only once bellow
           if[$flag == 1]
                 echo "
header
"; //echo only once above echo "hello"; //echo only once below if[$flag == 1] { echo "
footer
"; // Since you are done printing change the flag back to '0' $flag = 0; } //echo only once above } endforeach;

Echo content inside foreach only once


        
$get_status = array[];
foreach [$rows as $row]
{
    $get_status[] = $status;
}
extract[$get_status];
if [count[array_unique[$get_status]] === 1 && end[$get_status] === 'ONLINE']
{
    echo 'All systems are online';
}
if [count[$rows] > 0] 
{
    $get_status = array[];

    foreach [$rows as $row]
    {
        $service = $row->service;
        $details = $row->status_details;
        $status  = $row->status;
        $symbol  = '';
        
        // status image check       
        if [$status == 'ONLINE']
        {
            $symbol = 'online.png';
        }
        if [$status == 'OFFLINE']
        {
            $symbol = 'offline.png';
        }
        if [$status == 'DELAY']
        {
            $symbol = 'delay.png';
        }
        
        $get_status[] = $status;
    ?>
        

Chủ Đề