<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-comment">// This part of the code is unrelated to the main content, just for demonstration</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"Starting script execution...<br>"</span></span><span>;
</span><span><span class="hljs-title function_ invoke__">sleep</span></span><span>(</span><span><span class="hljs-number">1</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"Preparing content for output...<br>"</span></span><span>;
</span><span><span class="hljs-title function_ invoke__">sleep</span></span><span>(</span><span><span class="hljs-number">1</span></span><span>);
</span><span><span class="hljs-meta">?></span></span><span>
<hr>
<p></span><?php<br>
/*</p>
<ul>
<li>
<p>Title: How to Use flush() for Real-Time Content Output? Practical PHP Tips</p>
</li>
<li></li>
<li>
<p>By default, PHP sends webpage content to the browser only after the script completes.</p>
</li>
<li>
<p>This means if your program runs for a long time, users see no feedback while waiting, resulting in poor experience.</p>
</li>
<li>
<p>To address this, PHP provides the flush() function, which can push buffer content immediately to the browser, enabling “real-time output.”</p>
</li>
<li></li>
<li>
<p>This article explains how to use flush() in detail and offers practical tips to improve user experience in your projects.<br>
*/</p>
</li>
</ul>
<p>// 1. Basic principle of flush()<br>
// The flush() function forces PHP to send the current output buffer to the client.<br>
// Note that flush() does not clear the PHP output buffer; it only attempts to push its current contents to the client.</p>
<p>// 2. Using flush() with ob_flush()<br>
// In practice, since PHP often enables output buffering by default, calling flush() alone may not work.<br>
// Therefore, it is common to first call ob_flush() to clear PHP's buffer, then use flush() to push content to the browser.<br>
// Example:<br>
for ($i = 1; $i <= 5; $i++) {<br>
echo "Current progress: step $i completed...<br>";<br>
</span>ob_flush();<br>
flush();<br>
sleep(1); // Simulate time-consuming operation<br>
}</p>
<p>// 3. Effects of browser and server caching<br>
// Browser and server caching can prevent real-time display.<br>
// It is recommended to set HTTP headers to disable caching:<br>
header("Cache-Control: no-cache");<br>
header("Pragma: no-cache");</p>
<p>// 4. Disable gzip compression<br>
// If the server has gzip enabled, flush() may not work because compressed content must be complete to decompress.<br>
// You can disable gzip in PHP or server settings, or just for real-time output pages.</p>
<p>// 5. Use implicit flush<br>
// Enabling ini_set('implicit_flush', 1) automatically calls flush() after each output.<br>
// ob_implicit_flush(true) can achieve the same effect.</p>
<p>// 6. Pay attention to output format and content<br>
// Browsers need enough content to trigger rendering. Make sure output is sufficient or add padding spaces.</p>
<p>// 7. Practical example summary<br>
// Combining the above methods, a complete example is as follows:</p>
<p>/*<br>
<?php<br>
header("Content-Type: text/html; charset=utf-8");<br>
header("Cache-Control: no-cache");<br>
header("Pragma: no-cache");</p>
<p>ob_implicit_flush(true);<br>
while (@ob_end_flush()); // Close all output buffers</p>
<p>for ($i = 1; $i <= 10; $i++) {<br>
echo "Progress: $i / 10<br>";<br>
echo str_repeat(' ', 1024); // Pad output to help browser render immediately<br>
flush();<br>
sleep(1);<br>
}<br>
?><br>
*/</p>
<p>// Using the above approach can significantly improve the user experience for long-running scripts, allowing content to be seen in real time.</p>
<p data-is-last-node="" data-is-only-node="">// Conclusion<br>
// flush() is a highly practical technique in PHP, especially for progress indicators, real-time logging, and similar scenarios.<br>
// Understanding how it works and properly configuring server and PHP settings ensures maximum effectiveness.<br>
?><br>
</span>