Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jul 2006
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    2 variables with the same string doesn't match.

    I've transfered a bunch of names from a txt file into an array.
    Now i need to compare it against a string i have.
    When the strings are the same, for some reason it returns 'false' when i compare it.
    any idea what's the problem? the names are the same but i don't know
    why's the result is false....

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,854
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Please give some code/test-data.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    866
    Thanks
    7
    Thanked 30 Times in 29 Posts
    Sounds like you are using the "==" operator when you are comparing strings...and unfortunately..."==" operator does not always evaluate expressions on strings correctly.

    you should make use of the "strcmp" function when you are trying to comapre strings.

    here is an example
    PHP Code:
    $str1 "hello world";
    $str2 "Hello World";

    if( 
    strcmp$str1$str2 ) == ) {
       echo 
    "Strings are equal";
    } else{
       echo 
    "Strings are not equal";

    Note: strcmp is case sensitive...so, in the above example, you should expect "Strings are not equal"...if you want to use a case-insensitive alternative to strcmp, please use strncasecmp

    cheers,
    Ess
    Last edited by ess; 08-30-2007 at 12:44 PM.

  • #4
    New Coder
    Join Date
    Jul 2006
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?php
    $ourFileName 
    "file.txt";

    $fh fopen($ourFileName'r+') or die("Can't open file");

    $a 0;
    while (!
    feof($fh))
    {
    $names[$a] = fgets($fh);
    $a++;
    }

    /// for example you're looking for the username 'asdf'///
    $b 0;
    $as 'asdf';
    foreach (
    $names as $nnn)
    {
    if (
    $names[$b] == $as)
    {

    echo 
    "good";
    unset(
    $names[$b]);
    }
    echo 
    $names[$b];
    $b++;
    }
    fclose($fh);
    ?>
    when i add "echo $names[$b];" to the loop
    it prints everything as it is in the txt file
    but for some reason it doesn't execute the code inside the if statement
    when the array string is the same as the string i am trying to compare against.


    edit: thank you ess, i'll try if it works when i am at home.

  • #5
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    The string returned by fgets may include a newline at the end. Use rtrim on the returned value before you stuff it into the array.

    dumpfi

    Edit: And contrary to a comment made above the Equality Operator == does always evaluate expressions on strings correctly (according to the rules of the language).
    Last edited by dumpfi; 09-02-2007 at 03:53 PM.
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))Ż/.
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •