Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4082118
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:08:52+00:00 2026-05-20T18:08:52+00:00

In the snippet of code below, I’m using the strcasecmp() function. The function works

  • 0

In the snippet of code below, I’m using the strcasecmp() function. The function works fine for the first name match but does not for the last name.

For example if I have the name Joe Black in for names file and submit blanks for the name, the value of $xxx and $yyy are -3 and -6 respectively. If submit the name I get $xxx = 0 and $yyy = -1.

Can someone tell what I am missing or doing wrong??

Thanks

Chris

<?php 
$fname = $_POST["fname"]; 
$lname = $_POST["lname"];

print "First Name: " .$fname; 
print "< br/>"; 
print "Last Name: " . $lname;

$namesFile = "namesFile.txt";

$numLines = count(file($namesFile)); 
print "< br/>"; 
print "Lines: " . $numLines;

$fh = fopen ($psswrdFile, "r");

for ($i=1; $i <= $numLines; $i++) 
{ 
$lineData = fgets($fh); 
print "< br/>"; 
print $lineData;

list($firstName, $lastName) = explode(" ", $lineData);

print "First Name: " .$firstName;  

print "Last Name: " . $lastName;  

$xxx = strcasecmp($fname,$firstName);  

print "name " . $xxx;  

$yyy = strcasecmp($lname, $lastName);  

print "password" . $yyy;  
}

?>
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:08:52+00:00Added an answer on May 20, 2026 at 6:08 pm

    Your problem is that fgets() includes the end-of-line character in the line it reads. This means that your $linedata for “Joe Black” is actually “Joe Black\n”, i.e. it includes a newline. Therefore your $lastName ends up as “Black\n” rather than “Black”.

    As a quick fix, I would use stream_get_line() instead of fgets, specifying “\n” as the delimiter. This will work very similarly to fgets(), except it doesn’t read the delimiter into the string you get back.

    Or keep your fgets() and just strip the newline out of the string it gives you… As @kavisiegel mentions, trim() is an easy way of doing that (although technically I guess rtrim() will be faster!)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the code snippet below, however it's not working quite as I
This following code snippet works in FF, IE and Chrome. However It does not
How to send triimed data using $.get() see code snippet below: $(#txtSearch).keyup(function() { $.get(/controller/method/,
I am trying to get the below code snippet to compile. But it fails
In the snippet of code below, I'm using a class to get a reference
I have a small snippet of code below that I'm running using PellesC .
In the below code snippet, the intention is to take value for name from
I'm using the code snippet below to create a JFormattedTextField. When entering values via
The code snippet below search allow the user to match a string against three
In below code snippet, although pointer is not initialized the call is still made

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.