Replacing Do ... While Loops

I have the following code snippet taken from the PHP manual on curl_multi_ * entries:

$active = null;

do {
    $process = curl_multi_exec($curl, $active);
} while ($process === CURLM_CALL_MULTI_PERFORM);

while (($active >= 1) && ($process === CURLM_OK))
{
    if (curl_multi_select($curl, 3) != -1)
    {
        do {
            $process = curl_multi_exec($curl, $active);
        } while ($process === CURLM_CALL_MULTI_PERFORM);
    }
}

Now I really don't like to write ... while loops, and I was wondering what would be a better and shorter way to achieve the same, but without using such loops.

So far I have come up with a slightly longer version, but I'm not sure if it does the same or if it works the same as the original one:

while (true)
{
    $active = 1;
    $process = curl_multi_exec($curl, $active);

    if ($process === CURLM_OK)
    {
        while (($active >= 1) && (curl_multi_select($curl, 3) != -1))
        {
            $process = CURLM_CALL_MULTI_PERFORM;

            while ($process === CURLM_CALL_MULTI_PERFORM)
            {
                $process = curl_multi_exec($curl, $active);
            }
        }

        break;
    }

    else if ($process === CURLM_CALL_MULTI_PERFORM)
    {
        continue;
    }

    break;
}

Thanks in advance.

0
source share
3 answers
Cycles

Do..While While, , , Do..While . , Do..While , Do..While, While.

do {
    action();
} while(...)

:

action();
while(...) {
   action();
}

, :

$active = null;

$process = curl_multi_exec($curl, $active);
while ($process === CURLM_CALL_MULTI_PERFORM) {
    $process = curl_multi_exec($curl, $active);
}

while (($active >= 1) && ($process === CURLM_OK))
{
    if (curl_multi_select($curl, 3) != -1)
    {
       $process = curl_multi_exec($curl, $active);
       while ($process === CURLM_CALL_MULTI_PERFORM) {
           $process = curl_multi_exec($curl, $active);
       };
    }
}

, Do..While, , .

+5

do while while. , -, 1 , .

IMO, , .

+8

Will it do it?

$active = null;

$process = curl_multi_exec($curl, $active);
while ($process === CURLM_CALL_MULTI_PERFORM) {
    $process = curl_multi_exec($curl, $active);
};

$process = curl_multi_exec($curl, $active);
while (($active >= 1) && ($process === CURLM_OK)) {
    if (curl_multi_select($curl, 3) != -1) {
        while ($process === CURLM_CALL_MULTI_PERFORM) {
            $process = curl_multi_exec($curl, $active);
        } 
    }
}
+1
source

Source: https://habr.com/ru/post/1782882/


All Articles