Jump to content

To do... or not to do. That is the question. (File-reading/writing techniques)


kratsg

Recommended Posts

Ok, I have TWO methods currently to read a file line by line, since I store information about one specific user on each line, for example:

 

.htpasswd

user:pass
user1:pass1
user2:pass2
user3:pass3

 

Then I have two techniques for extracting a username/password pair:

 


Technique 1: fgets

$file = fopen(".htpasswd",'r');
while($line = fgets($file)){//while we can still read the next line in the file
$line = trim($line);//remove the line endings
list($user,$pass) = explode(':',$line);
echo "The username $user has a password $pass.<br>";
}
fclose($file);

 

Technique 2: foreach

$file = file(".htpasswd");
foreach($file as $line){
$line = trim($line);
list($user,pass) = explode(':',$line);
echo "The username $user has a password $pass.<br>";
}

 


 

My question is, which do YOU believe makes more sense? As for my belief, I like using fgets versus a foreach as fgets is a function for file-manipulation alone, while foreach is for both files (as shown above) and arrays.

 

Also, I have one final question (which fits in with the to do.. or not to do... idea). Look at how I wrote some of my functions, such as with the single-quotes and double-quotes. Am I better off sticking with double-quotes inside my functions, or should I use single-quotes? Same question applies for the echo function.

 


 

I would like to know what would be considered proper, or professional syntax for my above questions, and why.

Link to comment
Share on other sites

You have to use Double Quotes on your Echos because you are printing variables.

 

Change them to Single Quotes and see what the new result is.

 

 

Also, I have one final question (which fits in with the to do.. or not to do... idea). Look at how I wrote some of my functions, such as with the single-quotes and double-quotes. Am I better off sticking with double-quotes inside my functions, or should I use single-quotes? Same question applies for the echo function.

 

Link to comment
Share on other sites

No, I know that part, you can use single quotes with concatenating. But for ANY purpose, is it better to use single quotes or double quotes?

 

I want to be consistent in my coding, so I would like to see what other people are doing. It may be that we don't realize something as unimportant as this as PHP is soooo flexible here... I, for one, would like to sharpen up, so to speak.

Link to comment
Share on other sites

I think in regards to Quotes, its merely a preference of how you like to work with them and understanding how they work would be more important.

 

I think it would depend entirely on the code you are writing and what result you are expecting.  

Link to comment
Share on other sites

I think in regards to Quotes, its merely a preference of how you like to work with them and understanding how they work would be more important.

 

I think it would depend entirely on the code you are writing and what result you are expecting. 

I would assume the first because you're only dealing with a line at a time, whereas the second arrays the entire file before working with the individual lines.

 

See this topic on quote usage.

 

I agree with both, damn xD

 

I guess it really just matters on what you would like to achieve. I also slightly agree with the idea of fgets vs. foreach, but isn't it better to use a function that has more usability than another function that can only be used with fopen, etc...?

 

For example, both die and exit do almost the same thing, yet we almost always use die since it is more applicable in general.

Link to comment
Share on other sites

Too bad that thread is old, I wanted to make a comment about your line here and mention something about my wife:

 

PHP gave us both, so use them as they're intended. You wouldn't buy a tool set and always use a wrench for driving nails and ignore the hammer, even though they both work.

 

 

See this topic on quote usage.

Link to comment
Share on other sites

Supposedly, using single quotes is more efficient than using double quotes when you are not doing variable substitution.

 

As for the method of reading the file, I would use fgetcsv()

<?php
$file = fopen("mypasswd",'r');
while (($line = fgetcsv($file, 80, ":")) !== FALSE) {
        array_map("trim",$line);
        $user = $line[0];
        $pass = $line[1];
        echo "The username $user has a password $pass.\n";
}
fclose($file);
?>

 

Ken

Link to comment
Share on other sites

Supposedly, using single quotes is more efficient than using double quotes when you are not doing variable substitution.

 

As for the method of reading the file, I would use fgetcsv()

<?php
$file = fopen("mypasswd",'r');
while (($line = fgetcsv($file, 80, ":")) !== FALSE) {
        array_map("trim",$line);
        $user = $line[0];
        $pass = $line[1];
        echo "The username $user has a password $pass.\n";
}
fclose($file);
?>

 

Ken

 

Hmm, never actually saw this before as a function, NEAT! However, this will be efficient for my htpasswd file, yet for other files, I use varied delimiters:

 

.htgroups

group1:user1 user2 user3:access1 access2 access3

 

And etc, so as you can see, it gets a bit complicated there. I do like that method for simpler stuff, but I kinda prefer using a list() function when I explode an array just to simplify my life :-D

Link to comment
Share on other sites

I also slightly agree with the idea of fgets vs. foreach, but isn't it better to use a function that has more usability than another function that can only be used with fopen, etc...?

 

I don't think so. If fopen/fgets is faster, use it. file is probably fopen/fgets under the hood anyhow.

 

For example, both die and exit do almost the same thing, yet we almost always use die since it is more applicable in general.

 

According to the manual, die is equivalent to exit. I don't know which came first, but I assume they both exist because they're common to other languages, such as "exit" in shell scripting, and "die" in Perl. I assume everyone just uses die because it's one less character to type.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.