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

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.