<i id='QXKLv'><tr id='QXKLv'><dt id='QXKLv'><q id='QXKLv'><span id='QXKLv'><b id='QXKLv'><form id='QXKLv'><ins id='QXKLv'></ins><ul id='QXKLv'></ul><sub id='QXKLv'></sub></form><legend id='QXKLv'></legend><bdo id='QXKLv'><pre id='QXKLv'><center id='QXKLv'></center></pre></bdo></b><th id='QXKLv'></th></span></q></dt></tr></i><div id='QXKLv'><tfoot id='QXKLv'></tfoot><dl id='QXKLv'><fieldset id='QXKLv'></fieldset></dl></div>

      1. <tfoot id='QXKLv'></tfoot>

        <small id='QXKLv'></small><noframes id='QXKLv'>

        <legend id='QXKLv'><style id='QXKLv'><dir id='QXKLv'><q id='QXKLv'></q></dir></style></legend>
        • <bdo id='QXKLv'></bdo><ul id='QXKLv'></ul>
      2. PHP 是否在文件处理程序被垃圾收集后关闭文件?

        时间:2023-05-30
        <legend id='kMUXM'><style id='kMUXM'><dir id='kMUXM'><q id='kMUXM'></q></dir></style></legend>
      3. <i id='kMUXM'><tr id='kMUXM'><dt id='kMUXM'><q id='kMUXM'><span id='kMUXM'><b id='kMUXM'><form id='kMUXM'><ins id='kMUXM'></ins><ul id='kMUXM'></ul><sub id='kMUXM'></sub></form><legend id='kMUXM'></legend><bdo id='kMUXM'><pre id='kMUXM'><center id='kMUXM'></center></pre></bdo></b><th id='kMUXM'></th></span></q></dt></tr></i><div id='kMUXM'><tfoot id='kMUXM'></tfoot><dl id='kMUXM'><fieldset id='kMUXM'></fieldset></dl></div>

                <tbody id='kMUXM'></tbody>
              • <bdo id='kMUXM'></bdo><ul id='kMUXM'></ul>

                  <small id='kMUXM'></small><noframes id='kMUXM'>

                  <tfoot id='kMUXM'></tfoot>

                • 本文介绍了PHP 是否在文件处理程序被垃圾收集后关闭文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如果我有一个打开文件并读取一行的简短函数,我是否需要关闭文件?或者当执行退出函数并且 $fh 被垃圾收集时,PHP 会自动执行此操作吗?

                  If I have a short function that opens a file and reads a line, do I need to close the file? Or will PHP do this automatically when execution exits the function and $fh is garbage collected?

                  function first_line($file) {
                      $fh = fopen($file);
                      $first_line = fgets($fh);
                      fclose($fh);
                      return $first_line;
                  }
                  

                  可以简化为

                  function first_line($file) {
                      return fgets(fopen($file));
                  }
                  

                  这当然是理论上的,因为这段代码没有任何错误处理.

                  This is of course theoretical right now, as this code doesn't have any error handling.

                  推荐答案

                  一旦对资源的所有引用都被删除,PHP 就会自动运行资源析构函数.

                  PHP automatically runs the resource destructor as soon as all references to that resource are dropped.

                  由于 PHP 具有基于引用计数的垃圾收集,因此您可以相当确定这会尽早发生,在您的情况下,一旦 $fh 超出范围.

                  As PHP has a reference-counting based garbage collection you can be fairly sure that this happens as early as possible, in your case as soon as $fh goes out of scope.

                  在 PHP 5.4 之前 fclose 如果您试图关闭分配了两个以上引用的资源,实际上并没有做任何事情.

                  Before PHP 5.4 fclose didn't actually do anything if you tried to close a resource that had more than two references assigned to it.

                  这篇关于PHP 是否在文件处理程序被垃圾收集后关闭文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:从文件中读取最后一行 下一篇:在 MySQL 中找不到 outfile 创建的文件

                  相关文章

                    <bdo id='U4jxG'></bdo><ul id='U4jxG'></ul>

                    <tfoot id='U4jxG'></tfoot>

                    <legend id='U4jxG'><style id='U4jxG'><dir id='U4jxG'><q id='U4jxG'></q></dir></style></legend>

                    <small id='U4jxG'></small><noframes id='U4jxG'>

                    1. <i id='U4jxG'><tr id='U4jxG'><dt id='U4jxG'><q id='U4jxG'><span id='U4jxG'><b id='U4jxG'><form id='U4jxG'><ins id='U4jxG'></ins><ul id='U4jxG'></ul><sub id='U4jxG'></sub></form><legend id='U4jxG'></legend><bdo id='U4jxG'><pre id='U4jxG'><center id='U4jxG'></center></pre></bdo></b><th id='U4jxG'></th></span></q></dt></tr></i><div id='U4jxG'><tfoot id='U4jxG'></tfoot><dl id='U4jxG'><fieldset id='U4jxG'></fieldset></dl></div>