...

View Full Version : First If Is Always True



dudeman0501
07-09-2005, 11:24 PM
Alright guys, here's what I have:



//Other vars included but not shown
$item_categ = strtolower($_POST['categ']);


if ($item_categ = "miscellaneous") {
$item_categ = "misc";

} elseif ($item_categ = "currently parting") {
$item_categ = "parting";

} elseif ($item_categ = "vehicles for sale") {
$item_categ = "vehicles";

} elseif ($item_categ = "transfer cases") {
$item_categ = "transfercases";

} else {
$item_categ = $item_categ;

}


It doesn't matter what $item_categ equals, the code always sees the first if condition as true. Any ideas as to what the problem could be?

delinear
07-10-2005, 12:07 AM
if ($item_categ = "miscellaneous") {
$item_categ = "misc";
Should be:

if ($item_categ == "miscellaneous") {
$item_categ = "misc";
If you use a single = then you are assigning the value to the variable so it always returns true. Use == or === for comparing variables (that goes also for the rest of your if statements in that snippet), ie:

//Other vars included but not shown
$item_categ = strtolower($_POST['categ']);

if ($item_categ == "miscellaneous") {
$item_categ = "misc";

} elseif ($item_categ == "currently parting") {
$item_categ = "parting";

} elseif ($item_categ == "vehicles for sale") {
$item_categ = "vehicles";

} elseif ($item_categ == "transfer cases") {
$item_categ = "transfercases";

} else {
$item_categ = $item_categ;

}
Incidentally, a switch statement (http://uk2.php.net/switch) is probably better for this kind of thing, it's less cluttered and easier to see what's happening.

dudeman0501
07-10-2005, 12:27 AM
That's the second time the double equals has bitten me. Guess I should try to learn from my mistakes...

Anyway, thanks for pointing it out, and thanks for the switch link. I think I'll be able to use it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum