Foreach vs while for progressbar

Does foreach() outputs only after finishing all data, even if a print() is within foreach() ?
So this progressbar https://framework.zend.com/manual/2.4/en/modules/zend.progress-bar.html#basic-usage will not be shown as progressing within a foreach() only will be shown as complete after foreach() is finished?
and this works only with while() ?

That would really depend on how the ‘update’ method is constructed. it is possible that is suppressing the outcome; loops themselves do not suppress outputs by default. You may also want to check you logic. Perhaps there is no loop running.

WHILE vs FOREACH vs FOR

FOREACH:

  • requires an array ( it essentially creating a copy of the area then using the ‘each’ to itterate through it.

WHILE( condition){code}:
is a conditional loop. it will run the code within the brackets as long as the condition is true ( which mean that to prevent an infinite loop you need to make sure you change something that will at some point make the condition false!

DO{code }WHILE( condition):
is a variation of a WHILE loop where the conditions checked AFTER the iteration of the loop. This means the code will run AT LEAST once by necessity.

FOR( [vars;] condition;[ increment]){ code}
Describes a simple iteration. The loop itself moves a counter at each pass. You think of it LOOSELY as a WHILE loop with the iteration code built into it.

Hope that helps.

1 Like

If a print() is within foreach() it seems, it just starts printing after finishing the array loop once. How to output something immediately within foreach() with print? possible?

I guess you’ve missed reading this

Please post the code you have that is exhibiting the behavior you are describing.

what to post?

<?php
  $data = array(......); // it has 1000 key/value pairs.
  foreach ($data as $info) {
    print($info);
  }

Assuming you have nothing above the code, you’ll see how long it takes to load the page. for several minutes you see nothing but suddenly all data appears rather than seeing the data are printing one by one from start. Now got what I meant to say? Is it possible to enforce it to do printing one by one from start of loop?

Take a look on output buffering.

However, what you want to do is not feasible, as it will bring a huge load to reduce the output buffering that small (+ you would need to do it both in PHP and webserver side).

You mean what I want to do is not recommended?

What happens is that if it is set to low, you keep sending data from PHP to the backend webserver, which then send it to the user. If only PHP is changed, it will be buffered on next step before being sent, if both is set very low you spend a lot of server resources on keep sending data to the user. The best performance is if you synchronize those two to the same value, and that value is optimal for the content you send to your visitors. Today, around 4kb is not uncommon value.

Please also note, that most browsers today will not render the content before all of the html is downloaded. This means even if the data is sent to the browser, it will not show it before it has all of the content.

JavaScript would be better.

(abridged) PHP runs server side. The code gets processed and when done, output is sent to the browser.

you see nothing but suddenly all data appears rather than seeing the data are printing one by one from start. Now got what I meant to say? Is it possible to enforce it to do printing one by one from start of loop?

It’s kinda odd that it would take several minutes to output just 1000 value pairs… butI think you were expecting the wrong thing. (even w/o output buffering) a PHP script builds a page THEN serves it. You will not get a ‘terminal-like’ output behavior in with just PHP.

Actually that zend progressbar confused me. So we will not see a progressing progressbar with that php code on the doc I gave the link but we will see just a finished progressbar when page loaded.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.