<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-comment">// Cette partie du code n'a rien à voir avec le contenu du texte,Par exemple la démonstration uniquement</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"Commencez à exécuter le script...<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">"Préparer le contenu de sortie...<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>
</span><span><span class="hljs-meta"><?php</span></span><span>
<span class="hljs-comment">/*
* titre:Comment l'utiliser flush() Sortie de contenu en temps réel?PHP Analyse des compétences pratiques
*
* exister PHP milieu,Par défaut,Le contenu de la page Web est envoyé au navigateur client à la fois jusqu'à l'exécution du script.。
* Cela signifie si votre programme doit être exécuté pendant longtemps,用户exister等待时看不到任何反馈,Mauvaise expérience。
* Pour résoudre ce problème,PHP Fourni flush() fonction,Le contenu du tampon peut être envoyé instantanément au navigateur,Mettre en œuvre du contenu“Sortie en temps réel”。
*
* Cet article introduira en détail flush() Comment utiliser,并Combiné实战技巧帮助你exister项目milieu提升用户体验。
*/</span>
</span><span><span class="hljs-comment">// 1. flush() Le principe de base</span></span><span>
</span><span><span class="hljs-comment">// flush() fonction用于强制将 PHP Le contenu du tampon de sortie est envoyé au client。</span></span><span>
</span><span><span class="hljs-comment">// mais,Il convient de noter que,flush() Il ne sera pas effacé PHP Tampon de sortie,Il essaie juste de pousser le contenu tampon actuel au client。</span></span><span>
</span><span><span class="hljs-comment">// 2. Combiné ob_flush()</span></span><span>
</span><span><span class="hljs-comment">// exister实际应用milieu,parce que PHP La mise en mémoire tampon de sortie est activée par défaut(Output Buffering),Appelé séparément flush() Ne fonctionne probablement pas。</span></span><span>
</span><span><span class="hljs-comment">// donc,Coopération générale ob_flush() utiliser,Effacer d'abord PHP Tampon,再utiliser flush() Pousser au navigateur。</span></span><span>
</span><span><span class="hljs-comment">//</span></span><span>
</span><span><span class="hljs-comment">// Exemple:</span></span><span>
</span><span><span class="hljs-keyword">for</span></span><span> (</span><span><span class="hljs-variable">$i</span></span><span> = </span><span><span class="hljs-number">1</span></span><span>; </span><span><span class="hljs-variable">$i</span></span><span> <= </span><span><span class="hljs-number">5</span></span><span>; </span><span><span class="hljs-variable">$i</span></span><span>++) {
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"Progrès actuel:1 et 1 <span class="hljs-subst">$i</span></span></span><span> Étapes terminées...<br>";
</span><span><span class="hljs-title function_ invoke__">ob_flush</span></span><span>();
</span><span><span class="hljs-title function_ invoke__">flush</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-comment">// Opération de temps de simulation</span></span><span>
}
</span><span><span class="hljs-comment">// 3. L'impact de la fermeture du navigateur et du cache serveur</span></span><span>
</span><span><span class="hljs-comment">// Le mécanisme de cache des navigateurs et des serveurs peut entraver l'affichage en temps réel du contenu。</span></span><span>
</span><span><span class="hljs-comment">// Paramètres recommandés HTTP Désactiver le cache à la tête:</span></span><span>
</span><span><span class="hljs-title function_ invoke__">header</span></span><span>(</span><span><span class="hljs-string">"Cache-Control: no-cache"</span></span><span>);
</span><span><span class="hljs-title function_ invoke__">header</span></span><span>(</span><span><span class="hljs-string">"Pragma: no-cache"</span></span><span>);
</span><span><span class="hljs-comment">// 4. fermeture gzip compression</span></span><span>
</span><span><span class="hljs-comment">// Si le serveur est activé gzip compression,flush() Échec possible,因为compression后内容必须完整才能解压。</span></span><span>
</span><span><span class="hljs-comment">// 可以尝试exister PHP 配置或服务器配置milieufermeture gzip,或者针对Sortie en temps réel页面禁用。</span></span><span>
</span><span><span class="hljs-comment">// 5. utiliser隐式刷新</span></span><span>
</span><span><span class="hljs-comment">// passer ini_set('implicit_flush', 1) Un rafraîchissement implicite peut être activé,PHP exister每次输出后自动调用 flush()。</span></span><span>
</span><span><span class="hljs-comment">// 也可utiliser ob_implicit_flush(true) Obtenir le même effet。</span></span><span>
</span><span><span class="hljs-comment">// 6. Faites attention au format de sortie et au contenu</span></span><span>
</span><span><span class="hljs-comment">// Le navigateur a besoin de suffisamment de contenu pour déclencher le rendu,Il est recommandé de sortir suffisamment ou de le remplir avec certains espaces。</span></span><span>
</span><span><span class="hljs-comment">// 7. 实战Exemple总结</span></span><span>
</span><span><span class="hljs-comment">// Combiné上述方法,完整Exemple如下:</span></span><span>
<span class="hljs-comment">/*
<?php
header("Content-Type: text/html; charset=utf-8");
header("Cache-Control: no-cache");
header("Pragma: no-cache");
ob_implicit_flush(true);
while (</span></span><span><span class="hljs-doctag">@ob</span></span><span>_end_flush()); // fermeture所有输出缓冲
for ($i = 1; $i <= 10; $i++) {
echo "calendrier:$i / 10<br>";
echo str_repeat(' ', 1024); // Remplir la sortie,Aider le navigateur à rendre immédiatement
flush();
sleep(1);
}
?>
*/
</span><span><span class="hljs-comment">// passer以上方式,Peut améliorer considérablement l'expérience d'attente pour les utilisateurs pendant que les scripts fonctionnent pendant longtemps,Voir les mises à jour de contenu en temps réel。</span></span><span>
</span><span><span class="hljs-comment">// Conclusion</span></span><span>
</span><span><span class="hljs-comment">// flush() exister PHP 实战milieu是一个非常实用的技巧,尤其适合calendrier显示、日志Sortie en temps réel等场景。</span></span><span>
</span><span><span class="hljs-comment">// Comprendre comment ça marche,Configurez correctement le serveur et PHP installation,Seulement en atteignant l'effet maximum。</span></span><span>
</span><span><span class="hljs-meta">?></span></span><span>
</span></span>