String Comparison

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BOMEz
    New Member
    • Dec 2007
    • 40

    #1

    String Comparison

    I'm trying to compare two strings with the following bit of code:
    [PHP]
    for($i = 0; $i<$size; $i++)
    {

    if("$descriptio n[$i]" == $_POST['cat'])
    {
    echo "SUCCESS";
    }
    else
    {
    echo "FAIL";
    }
    }
    [/PHP]

    But I keep getting a string of FAIL's

    $description[$i] is taking lines of text out of a text file (about 5 lines)
    $_POST['cat'] contains a selection from a drop down menu, using information created from the same text file.

    If I were to put in the line of code:
    [PHP] echo $description[$i] ." ?= ". $_POST['cat']. "<BR>";[/PHP]
    right before my if statement I get:
    Help Desk hours ?= Help Desk hours
    sales Call ?= Help Desk hours
    Help with buying new laptop ?= Help Desk hours
    location of software ?= Help Desk hours
    So the strings of text match, but it never returns SUCCESS, unless I type in "Help Desk hours" in place of $description[$i] (assuming I choose Help Desk hours to send into $_POST['cat'] )

    Where am I going wrong with this?
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, BOMEz.

    Try removing the quotes around $description[$i]. PHP is a loosely-typed language, so you don't need to explicitly cast as a string before comparing.

    Also try comparing trim()'d values (http://php.net/trim).

    Comment

    Working...