1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

more then one in "if"

Discussion in 'PHP' started by zodiac, Apr 21, 2008.

  1. #1
    i want to list only certain categories that are in the database.
    if ($category['name'] == "cat1")
    PHP:
    what is the right syntax to add cat2?
     
    zodiac, Apr 21, 2008 IP
  2. ToddMicheau

    ToddMicheau Active Member

    Messages:
    183
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Would an ElseIf be what your looking for?

    if ($category['name'] == "cat1"){
    	;;
    }else if ($category['name'] == "cat2"){
    	;;
    }else if ($category['name'] == "cat3"){
    	;;
    }
    PHP:
    That is more than one If statment. But I think that a Switch might be a better choice:

    switch($category['name']){
    	case "cat1":
    	//Do stuff
    	break;
    	case "cat2":
    	//Do more stuff
    	break;
    	case "cat3":
    	//Etc
    	break;
    	default:
    	//if none of the conditions are met do this.
    }
    PHP:
    Otherwise if you are looking to test two conditions then:

    if ($category['name'] == "cat1" || $category['name'] == "cat2"){
    	//Do stuff.
    }
    PHP:
    (Will execute if either one is true, or: )

    if ($category['name'] == "cat1" && $category['name'] == "cat2"){
    	//Do stuff.
    }
    PHP:
    (Will execute only if both are true.)
     
    ToddMicheau, Apr 21, 2008 IP
    zodiac likes this.
  3. zodiac

    zodiac Peon

    Messages:
    2,661
    Likes Received:
    82
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thats the one i was looking for.
    i knew the way it should be,but i couldn't remember the way it is coded.
    thanks.
     
    zodiac, Apr 21, 2008 IP
  4. ToddMicheau

    ToddMicheau Active Member

    Messages:
    183
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #4
    Haha, no problem- you confused me by asking for more If's =]
     
    ToddMicheau, Apr 21, 2008 IP
  5. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #5
    If this is going to change often and/or get more complex, use an array or regex instead of hard coding each individual case.

    Array:
    if (in_array($category['name'], array('cat1', 'cat2')) { 
    PHP:
    Regex:
    if (preg_match('/cat[12]/', $category['name'])) { 
    PHP:
     
    krt, Apr 21, 2008 IP