• <bdo id='EC8M2'></bdo><ul id='EC8M2'></ul>

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

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

    1. <tfoot id='EC8M2'></tfoot>
    2. <legend id='EC8M2'><style id='EC8M2'><dir id='EC8M2'><q id='EC8M2'></q></dir></style></legend>
    3. PHP 是否在文件处理程序被垃圾收集后关闭文件

      时间:2023-10-02

        <i id='ehPzw'><tr id='ehPzw'><dt id='ehPzw'><q id='ehPzw'><span id='ehPzw'><b id='ehPzw'><form id='ehPzw'><ins id='ehPzw'></ins><ul id='ehPzw'></ul><sub id='ehPzw'></sub></form><legend id='ehPzw'></legend><bdo id='ehPzw'><pre id='ehPzw'><center id='ehPzw'></center></pre></bdo></b><th id='ehPzw'></th></span></q></dt></tr></i><div id='ehPzw'><tfoot id='ehPzw'></tfoot><dl id='ehPzw'><fieldset id='ehPzw'></fieldset></dl></div>
      1. <tfoot id='ehPzw'></tfoot>
            • <bdo id='ehPzw'></bdo><ul id='ehPzw'></ul>
                  <tbody id='ehPzw'></tbody>
                <legend id='ehPzw'><style id='ehPzw'><dir id='ehPzw'><q id='ehPzw'></q></dir></style></legend>
              • <small id='ehPzw'></small><noframes id='ehPzw'>

                本文介绍了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 是否在文件处理程序被垃圾收集后关闭文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

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

                相关文章

                最新文章

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

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

                  • <bdo id='FHy0V'></bdo><ul id='FHy0V'></ul>
                  1. <tfoot id='FHy0V'></tfoot>