Kaylub Posted May 26, 2010 Share Posted May 26, 2010 Sorry, I'm not an experienced programmer at all and am having some troubles with a simple piece of code. For some reason the echo never fires off. I know the $'s are correct, because when I test them with an echo outside of the if statement, they both return the correct value. How come I can't get them to compare eachother using an if statement? Could anyone tell me why this piece of code isn't working: " <?php function NameChecker ($textfieldUserNameSignUp) { $theData = "Initialize"; $myFile = "../../USER_INFO/USERNAMES.txt"; $fh = fopen($myFile, 'r'); while ($theData <> "") { $theData = fgets($fh); if ($theData==$textfieldUserNameSignUp) { echo "Match!";} } fclose($fh);} NameChecker ($_REQUEST['textfieldUserNameSignUp']); ?> " Thanks for helping a noob. Quote Link to comment Share on other sites More sharing options...
Kaylub Posted May 26, 2010 Author Share Posted May 26, 2010 Can no one answer this? Is my programming far to stupid? heh Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted May 26, 2010 Share Posted May 26, 2010 You didn't wait very long to bump your question. Wait at least an hour or two. I would read file into an array using the file function outside the function and pass the array to the function. That way you're only reading the file once: <?php function NameChecker ($textfieldUserNameSignUp,$file_array) { foreach($file_array as $data) { if (trim($data) == $textfieldUserNameSignUp) { echo "Match!"; } } } $file = file('../../USER_INFO/USERNAMES.txt'); NameChecker ($_REQUEST['textfieldUserNameSignUp'],$file); ?> The reason your original code probably didn't work is that each line read in from the file probably contained an End-of-line character that should be trimmed before doing the compare. Ken Quote Link to comment 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.