Jump to content

Help with some very simple code


Kaylub

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/203013-help-with-some-very-simple-code/
Share on other sites

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.