if..elseif..else statement with 'OR' conditions

Discussion in 'PHP' started by fwitkowski, May 31, 2011.

  1. #1
    I'm working on custom breadcrumbs in drupal website and have to display one word based on a few categories
    PHP instruction like:

    <?php if (($ppp=='bracelets') || ($ppp=='earrings') || ($ppp=='brooches') || ($ppp=='necklaces')): print_r('JEWERLY');
    elseif (($ppp=='combs') OR ($ppp=='feathers') OR ($ppp=='hairpins') OR ($ppp=='squiggles') OR ($ppp=='tiaras')): print_r('HEADPIECES');
    else: print_r('OTHER');
    endif; ?>


    always gives me result 'OTHER'

    For exaple I put before <?php print_r($ppp);?>
    and get
    Bracelets OTHER
    or
    Feathers OTHER

    What do I do wrong?

    Thanks
     
    fwitkowski, May 31, 2011 IP
  2. ssmm987

    ssmm987 Member

    Messages:
    180
    Likes Received:
    4
    Best Answers:
    3
    Trophy Points:
    43
    #2
    Bracelets (capital B) does not equal bracelets(small b), use the strtolower function instead. You could just add the line
    $ppp=strtolower($ppp)
    PHP:
    before this script.

    Also, it may be easier to use the in array function when comparing a string to a lot of other strings:
    
    if(in_array($ppp,array('brachelets','earrings','brooches','necklaces'))):
    	print_r('JEWERLY');
    elseif(in_array($ppp,array('combs','feathers','hairpins','squiggles','tiaras'))):
    	print_r('HEADPIECES');
    else:
    	print_r('OTHER');
    endif;
    
    PHP:
    in_array is case insensitive, therefor you don't need to lowercase the needle.
     
    ssmm987, May 31, 2011 IP