...

View Full Version : if statment wont work with fgets



alcool9999
08-24-2009, 03:41 PM
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";
}
}

Phil Jackson
08-24-2009, 04:07 PM
not sure if this helps but try


trim($user1) == trim($useri)

funnymoney
08-24-2009, 04:17 PM
can you add this line of 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";
}
}

Fou-Lu
08-24-2009, 04:55 PM
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

$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.

alcool9999
08-25-2009, 02:06 PM
Thanks for the replys i try them now.

alcool9999
08-25-2009, 02:35 PM
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.

Fou-Lu
08-25-2009, 05:50 PM
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?

Phil Jackson
08-25-2009, 06:32 PM
No props, i thought it might be a white-space-case.

tailender1
08-25-2009, 07:48 PM
i normally use strpos. strcmp never worked for me correctly.

Fou-Lu
08-26-2009, 08:59 AM
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:


$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:


$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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum