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 3 of 3
  1. #1
    New Coder
    Join Date
    Jan 2005
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    First If Is Always True

    Alright guys, here's what I have:

    PHP Code:
    //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?

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    if ($item_categ "miscellaneous") { 
        
    $item_categ "misc"
    Should be:
    PHP Code:
    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:
    PHP Code:
     //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 is probably better for this kind of thing, it's less cluttered and easier to see what's happening.
    Last edited by delinear; 07-10-2005 at 12:10 AM.

  • #3
    New Coder
    Join Date
    Jan 2005
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

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