Home > Backend Development > PHP Tutorial > 循环大量数据导致内存超出,不增加内存怎么解决该有关问题

循环大量数据导致内存超出,不增加内存怎么解决该有关问题

WBOY
Release: 2016-06-13 12:20:25
Original
920 people have browsed it

循环大量数据导致内存超出,不增加内存如何解决该问题
我在生成HTML文件时,由于数据量较大,无数次的 fopen fwrite fclose,文件还没生成完呢,就提示内存超出。
我的HTML生成方式是,读入缓存,再写入文件

<br />ob_start ();<br />		$temp = ob_get_contents ();<br />		ob_end_clean ();<br />
Copy after login

我unset(temp) 也不起作用,这个问题到底该怎么解决。不知那些CMS又是怎么生成HTML的,再大的数据也不会内存超出
------解决思路----------------------
这种情况要分批执行,上个代码,你马上就明白其中的原理了
$total = 10;<br />$s = 0;<br />if (isset ( $_GET ['s'] )) {<br />        $s = &$_GET ['s'];<br />}<br />$per = $s + 5; // 每次循环5条<br />if ($per > $total) { // 如果下次循环会超过总数,就让他只能循环到总数<br />        $per = &$total;<br />}<br />while ( $s < $per ) {<br />        echo ++ $s;<br />        echo '<br>';<br />}<br />echo '<hr>';<br />echo $s;<br />if ($s < $total) {<br />        echo "<script>window.location.href='?s=$s'</script>";<br />}
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template