Loop statement output problems

Source: Internet
Author: User
The problem with loop statement output is that you can use for + switch to output a specified statement, for ($ I = 1; $ I & lt; = 10; $ I ++) {& nbsp; switch ($ I) {case1: echo & quot; 1 & quot; echo & quot; & lt; BR & gt; & quot; & nbsp; break; loop statement output
You can use for + switch to output a specified statement,

For ($ I = 1; $ I <= 10; $ I ++)
{
Switch ($ I)
{
Case 1:
Echo "1 ";
Echo"
";

Break;
Case 2:
Echo "2 ";
Echo"
";
Break;
Case 3:
Echo "3 ";
Echo"
";
Break;
Case 4:
Echo "4 ";
Echo"
";
Break;
Case 5:
Echo "5 ";
Echo"
";

Default:
Echo "OK ";
}

For example, the preceding statement can output a number when I is equal to a certain number,
But now 1-5 numbers are instantly output. how can I output these five numbers every several seconds? for example, output 2 every five seconds after output 1, output 3 in 5 seconds
Another problem is that the above code can only be output once. that is to say, 1-5 numbers are not output after being Output. Is there any way to output 1-5 numbers cyclically?
That is to say, after 1-5 is output, I is converted to 1 and then output again.


------ Solution --------------------
1. Add sleep (5)

2. switch ($ I % 5)
------ Solution --------------------
If you add a timer to him and you find it useful again, the problem is that you are not aware that php is processing all the code and then outputting the result.
If you want to display it like a typewriter, use js to control it.

Discussion

Using the sleep function,
The result will still be output at a time, and no output will be made. after 5 seconds, a form will be output again.
Changing the ajax call time to 30 seconds is the same and cannot be output one by one.

------ Solution --------------------
Latency 5 seconds output, required
Flush ();
Ob_flush ();
Sleep (5 );

For ie browsers
Echo $ I. str_repeat ('', 256 );
That is, each output cannot be less than 256 bytes.

Output_buffering = off


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.