Lectrician Posted August 25, 2014 Share Posted August 25, 2014 Ok. I am asking some basic type questions I guess! When you open a file with a filehandle, is it safe to use the same file handle a couple times in a script, for different files, assuming one is closed before the other opened? I use $fh a lot in my scripts, and wonder if this is bad practice? I copy and paste code quite often to speed up my writing, and retain this filehandle usually. Quote Link to comment https://forums.phpfreaks.com/topic/290646-file-handles/ Share on other sites More sharing options...
ginerjm Posted August 25, 2014 Share Posted August 25, 2014 If the var is not being used, why not use it again? Just don't get confused should be opening two files at the same time. It's no different than using $I as a counter multiple times in a script. Quote Link to comment https://forums.phpfreaks.com/topic/290646-file-handles/#findComment-1488902 Share on other sites More sharing options...
CroNiX Posted August 25, 2014 Share Posted August 25, 2014 No, that's actually probably best as whenever you create a new variable it uses memory, so reusing them would save a bit of ram. Quote Link to comment https://forums.phpfreaks.com/topic/290646-file-handles/#findComment-1488903 Share on other sites More sharing options...
Jacques1 Posted August 25, 2014 Share Posted August 25, 2014 You can reuse a variable as often as you want. However, “fn” isn't exactly a particularly meaningful name. This may be OK if your code is so short that one can derive the meaning from the context, but it's generally a good idea to give your variables proper names which speak for themselves. You do a lot of copypasting? That's a bad sign. Use functions to avoid repeating the same logic over and over again. Note that PHP itself has a lot of functions that may be useful. For example, cumbersome low-level file access can often be replaced with a single file_get_contents() or file_put_contents() call. Quote Link to comment https://forums.phpfreaks.com/topic/290646-file-handles/#findComment-1488904 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.