Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Aug 2009
    Posts
    84
    Thanks
    10
    Thanked 0 Times in 0 Posts

    if statment wont work with fgets

    hi i have a for loop that uses fgets to read the file line by line. Everytime it assigns the line to the same variable called $user1. so it overwrites it.
    i put an if statment in the loop to check if the line just read matches another but it wont work if always returns false when it should return true 1/5.

    any help apreciated. thanks

    code:

    $useri="screen";
    $line = count(file('username.txt'));
    $myFile = "username.txt";
    $fh = fopen($myFile, 'r');
    for ($i=1; $i<=$line; $i++)
    {
    $user1 = fgets($fh);
    if ($user1 == $useri)
    {
    echo " Match";
    }
    }

  • #2
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    not sure if this helps but try

    PHP Code:
    trim($user1) == trim($useri

  • Users who have thanked Phil Jackson for this post:

    alcool9999 (08-25-2009)

  • #3
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    can you add this line of code

    PHP Code:
    $useri="screen";
    $line count(file('username.txt'));
    #this line
    print_r(file('username.txt'));
    #
    $myFile "username.txt";
    $fh fopen($myFile'r');
    for (
    $i=1$i<=$line$i++)
    {
    $user1 fgets($fh);
    if (
    $user1 == $useri)
    {
    echo 
    " Match";
    }    


  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    What the heck?
    Don't combine an fgets() with a file(). It just doesn't make any sense.
    Choose between the two. File will work fine for you so long as you don't exceed about 20MB in size. If it has potential to become larger, use fgets, since file is likely to fail:
    PHP Code:
    <?php

    $useri 
    'screen';
    $myFile 'usernames.txt';
    $fh = @fopen($myFile);
    if (
    $fh)
    {
        while (!
    feof($fh))
        {
            
    $user1 fgets($fh);
            
    $user1 trim($user1); // I don't think fgets returns an addressof.
            
    if (strcmp($user1$useri) === 0)
            {
                print 
    'Match found.';
            }
        }
        
    fclose($fh);
    }
    If you want to halt the loop, you can either use a break or you can add a condition to the while. I find that people tend to like seeing the additional while condition, though I prefer the break since it will take less memory to execute.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    New Coder
    Join Date
    Aug 2009
    Posts
    84
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Thanks for the replys i try them now.

  • #6
    New Coder
    Join Date
    Aug 2009
    Posts
    84
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Thankyou so much phil it worked.

    Fou-Lu i couldnt seem to get yours to work but thank you for the code as it helped me adapt mine.

    Thankyou so much.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by alcool9999 View Post
    Thankyou so much phil it worked.

    Fou-Lu i couldnt seem to get yours to work but thank you for the code as it helped me adapt mine.

    Thankyou so much.
    NP.
    Problem with mine is here: $fh = @fopen($myFile, 'r');. Guess we need a mode to open the file with eh?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #8
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    No props, i thought it might be a white-space-case.

  • #9
    New Coder
    Join Date
    Aug 2009
    Posts
    26
    Thanks
    1
    Thanked 3 Times in 3 Posts
    i normally use strpos. strcmp never worked for me correctly.

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Don't use strpos to compare strings.
    strpos works by finding the offset of a string within another string, but is one of the PHP functions where you need to use explict checking to determine if the string is actually located within it:
    PHP Code:
    $s1 'catastrophic';
    $s2 'cat';
    if (!
    strpos($s1$s2))
    {
        
    printf("%s is not found in %s\n"$s2$s1);

    That will return false and print out that 'cat is not found in catastrophic' when it clearly is since the 0th location will correspond to false in PHP.
    It also doesn't compare full strings, only parts, so its useful for searching but not for comparing.

    I find that strcmp problems often stem from the usage of the return result. It seems almost illogical because this is valid:
    PHP Code:
    $s1 'cat';
    $s2 'cat';
    if (!
    strcmp($s1$s2))
    {
        
    printf("%s is equal to %s\n"$s1$s2);

    The return result for equalitity is 0 which equates to false.
    On the plus side, if you don't like using strcmp, (string) === (string) is also valid.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •