By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,723 Members | 1,891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,723 IT Pros & Developers. It's quick & easy.

String Comparison

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

if("$description[$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?
Oct 6 '08 #1

✓ answered by pbmods

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

Share this Question
Share on Google+
1 Reply


pbmods
Expert 5K+
P: 5,821
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).
Oct 6 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.