note 13099 deleted from function.parse-url by jimw
| From: | jimw@php.net | Date: | Fri, 21 Dec 2001 00:40:24 +0000 |
| Subject: | note 13099 deleted from function.parse-url by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-23663@lists.php.net to get a copy of this message | ||
Files which are opened with fopen() get automatically closed when their last reference is lost.
So you don't need to store the file pointer into a variable if you just need to check that the
file can be opened. For example, the following code does not leak:
if (fopen($filename, 'a')) {
// OK file exists and can be appended
} else {
// File may exist, but it cannot be modified
}
And you don't need to close a file on return of a function which assigned the file pointer to a
variable which was not declared as global in the function:
function log_to($filename,$line){
$er=error_reporting(0);//avoid PHP warning if file can't be open
if (($fp = fopen($filename, 'a'))
fputs($line, $fp);
error_reporting($er);
}
When assigning the file pointer to a global variable, the file is closed when the global variable
scope ends, i.e. when the script terminates.