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.

Multiple conditions in if statement

Discussion in 'PHP' started by egdcltd, Nov 9, 2006.

  1. #1
    How do I write an if statement with multiple conditions needing to be true?

    ie:

    if (condition1=a AND condition2=b AND condition3=c)
    {
    }
    else
    {
    }

    I'm sure it can be done, I just can't find instructions on how to do it.
     
    egdcltd, Nov 9, 2006 IP
  2. KC TAN

    KC TAN Well-Known Member

    Messages:
    4,794
    Likes Received:
    352
    Best Answers:
    0
    Trophy Points:
    155
    #2
    
    if ($x=='x' && $y=='y' && $z=='z'){
    // do if matches
    }else {
    //do otherwise
    }
    
    PHP:
    Hope that this is what you want :)
     
    KC TAN, Nov 9, 2006 IP
    egdcltd likes this.
  3. egdcltd

    egdcltd Peon

    Messages:
    692
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes, that was exactly what I was looking for. Thanks.
     
    egdcltd, Nov 10, 2006 IP
  4. tyler_durden

    tyler_durden Peon

    Messages:
    347
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yeah I know this is a very old thread, but I need help on something very similar to this. I need multiple fields to match, BUT some fields could be of multiple variables. Here is an example

    if ($x=='x' && $y=='y' or $y=='y' or $y=='a'){
    // do if matches
    }else {
    //do otherwise
    }
    PHP:
    In this example the $x needs to equal 'x', but $y needs to equal ''y' or 'a'. I have tried using the 'or' statement and I do not get errors, BUT the code does not function properly. PHP is not my strongest area, and i hve spent 2 hours on this now.
     
    tyler_durden, Jan 23, 2010 IP
  5. tyler_durden

    tyler_durden Peon

    Messages:
    347
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Geesh, nevermind. That or statement does work now. I think I was using a wrong variable in my testing.
     
    tyler_durden, Jan 23, 2010 IP
  6. tyler_durden

    tyler_durden Peon

    Messages:
    347
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #6
    After more testing I have found the above code DOES NOT WORK PROPERLY after all.

    I could do
    if ($x=='x' && $y=='y'){
    // do if matches
    elseif  ($x=='x' && $y=='a')
    // do if matches
    PHP:
    But I have about 40 variables AND the "do if" statement is quite lengthy so this would be some monsterly bloated code. $x and $y are the only two variables, but each could be one of 20 or so different terms that need to match.

    Any sugestions?
     
    Last edited: Jan 23, 2010
    tyler_durden, Jan 23, 2010 IP
  7. danx10

    danx10 Peon

    Messages:
    1,181
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #7
    instead of using; 'or' use '||'

    if ($x=='x' && $y=='y' || $y=='y' || $y=='a'){
    // do if matches
    }else {
    //do otherwise
    }
    PHP:
     
    danx10, Jan 23, 2010 IP
  8. tyler_durden

    tyler_durden Peon

    Messages:
    347
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanks for the tips, but I've found both '||' and 'or' do work the same, but there must be something wrong with my syntax. I currently have:

    if (($name=='Corvette') and ($year=='1953' || '1954' || '1955' || '1956' || '1957' || '1958' || '1959' || '1960' || '1961' || '1962'))
    {
    echo "Generation 1 Corvette";
    }
    elseif (($name=='Corvette') and ($year=='1963' || '1964' || '1965' || '1966' || '1967'))
    {
    echo "Generation 2 Corvette";
    }
    else
    {
    echo "No Generations";
    }
    
    
    PHP:
    This sort of works, BUT if I enter a 1940 Corvette into the database (Yes I know there's no 1940 Corvette), it still throws the Generation 1 code out. I want it to output the else code of "No Generations". What am I doing wrong?
     
    tyler_durden, Jan 26, 2010 IP
  9. danx10

    danx10 Peon

    Messages:
    1,181
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #9
    Do it this way... (untested).

    <?php
    
    $name = "Corvette";
    
    $year = "1940";
    
    if ($name=='Corvette' && $year=='1953' || $name=='Corvette' && $year=='1954' || $name=='Corvette' && $year=='1955' || $name=='Corvette' && $year=='1956' || $name=='Corvette' && $year=='1957' || $name=='Corvette' && $year=='1958' || $name=='Corvette' && $year=='1959' || $name=='Corvette' && $year=='1960' || $name=='Corvette' && $year=='1961' || $name=='Corvette' && $year=='1962'){
    echo "Generation 1 Corvette";
    }
    elseif ($name=='Corvette' && $year=='1963' || $name=='Corvette' && $year=='1964' || $name=='Corvette' && $year=='1965' || $name=='Corvette' && $year=='1966' || $name=='Corvette' && $year=='1967')
    {
    echo "Generation 2 Corvette";
    }
    else
    {
    echo "No Generations";
    }
    
    ?>
    PHP:
     
    danx10, Jan 26, 2010 IP
  10. tyler_durden

    tyler_durden Peon

    Messages:
    347
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #10
    danx10, you're my hero of the day. That worked perfectly. Thanks again!
     
    tyler_durden, Jan 26, 2010 IP
  11. intellilogic

    intellilogic Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    I responded as quickly as I could! LOL (Years later). I found this on a google search and thought others might as well, so i figured I'd help them avoid some of the mistakes here.

    The above methods aren't kind to performance and are teaching you bad logic. Here is how I would go about it.

    <?
    //For easier testing I used GET, so you can do:  file.php?name=Corvette&year=1966
    //Works just as well if you do $name="Corvette" or get the name from somewhere else
    $name=$_GET['name'];
    $year=$_GET['year'];
    
    //!$var will check to see if they are null, blank, missing, etc...
    if(!$name||!$year||!is_numeric($year)){$message="Invalid data";}else{
    	if ($name=='Corvette'){
    		if($year <= 1952){$message="Whoa, that's old, they didn't make a $name then!";}
    		elseif($year >= 1953 && $year <= 1962){$message="Generation 1 $name";}
    		elseif($year >= 1963 && $year <= 1967){$message="Generation 2 $name";}
    		elseif($year >= 1968 && $year <= 1982){$message="Generation 3 $name";}
    		elseif($year >= 1984 && $year <= 1996){$message="Generation 4 $name";}
    		elseif($year >= 1997 && $year <= 2004){$message="Generation 5 $name";}
    		elseif($year >= 2005 && $year <= (date('Y')+1)){$message="Generation 6 $name";}
    		elseif($year >= (date('Y')+2)){$message="Spaceship?  Typo? it's only ".date('Y').", next years model is only ".(date('Y')+1)."...";}
    	}
    	elseif($name=='nextname'){
    	}
    	else{$message="Name not entered!";}
    }
    echo $message;
    ?>
    PHP:
    I suppose you could also do this:
    		if($year=='1953'||$year=='1954'||$year=='1955'||$year=='1956'||$year=='1957'||$year=='1958'||$year=='1959'||$year=='1960'||$year=='1961'||$year=='1962'){$message="Generation 1 $name";}
    		elseif($year=='1963'||$year=='1964'||$year=='1965'||$year=='1966'||$year=='1967'){$message="Generation 2 $name";}
    PHP:
     
    intellilogic, May 11, 2011 IP
  12. billmix

    billmix Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Consider a regular expression to check through that many conditions.

    
    if(preg_match('(195[3-9]|196[0-2])', $year)){
         $message="Generation 1 $name";
    }elseif(preg_match('(196[3-7])', $year)){
         $message="Generation 2 $name";
    }
    
    PHP:
     
    billmix, Jun 8, 2011 IP
  13. dhtseany

    dhtseany Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Ha, you're right, I registered just to respond... Found it from Google. :)

    Anyways, in my case I'm trying to check if 2 isset()'s are true then do something, or move on. So far I have this:

    
    if ((isset($_GET['e'])) && (isset($_POST['submit']))) {
        echo "E was set and the script recognized that a submit happened.";
        }
        else {
    
    PHP:
    Basically, I'm trying to be lazy and reuse forms, used initially to create and insert the data into a $_SESSION array, and edit them. Recalling the $_SESSION data back into the form fields was fairly easy, but the point is to come back to this page to edit the data and submit the changes back into the array. So, I decided the make a link to that first page but with the addition of ?e=e to symbolize I'm in a pseudo "edit mode" and since that is TRUE, then the array_push() (haven't gotten that far, probably a similar command) puts the data back into the array. Because I'll need a modified push, I need to A) check if the form is in "edit mode" and B) alter the array data rather than start over from scratch. Plus, I'll also like it to jump to another page when it's successfully altered the array's data.

    Peace,
    Sean
     
    dhtseany, Aug 23, 2011 IP
  14. ccollister

    ccollister Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Hello,

    I am trying to do something similar to show the active page in a secondary menu on a website. I currently have the following:

    <?php
    if ($thisPage=='mid-market-overview' or $thisPage=='investment-strategies' or $thisPage=='healthcare-finance' or $thisPage=='restaurant-finance' or $thisPage=='software-finance' or $thisPage=='recent-financings' or $thisPage=='professionals' or $thisPage=='testimonials') {
    echo 'Middle Market Finance';}
    else {
    echo '<a href="../middle-market-finance/">Middle Market Finance</a>';} ?>

    I've tried replacing the "or" with "||" but it still doesn't work. I used the same function where there are only two variables and it works:

    <?php
    if ($thisPage=='bsl-overview' or $thisPage=='bsl-professionals') {
    echo 'BSL Asset Management';}
    else {
    echo '<a href="../bsl-asset-management/">BSL Asset Management</a>';} ?>

    Can anyone help me figure out why this isn't working?

    Thanks!
    Christine
     
    ccollister, Mar 14, 2012 IP
  15. egdcltd

    egdcltd Peon

    Messages:
    692
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Wow, long time since I originally posted this thread.

    I'd switch to using || anyway, but have you tried wrapping your conditions in brackets?

    (($thisPage=='mid-market-overview') || ($thisPage=='investment-strategies'))
    PHP:
     
    egdcltd, Mar 14, 2012 IP
  16. ccollister

    ccollister Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Yup, but I did a Google search for "if else multiple conditions" and this came up and seemed highly relevant.

    I tried the brackets. It works for the function with two conditions, but not the function with more than that. It actually works for $thisPage=='mid-market-overview' but stops after that. Driving me nuts.
     
    ccollister, Mar 14, 2012 IP
  17. Louie55

    Louie55 Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17

    I know this thread is old and so is the above post, but I thought I would show a different way of coding the above in Optimized non-redundant code for future viewers of this page. It's amazing how complicated and redundant others have made this simple task. Here is a working example of an optimized way of doing the same thing:

    
    //requires $name variable and $year variable to be passed. Does no data validation or whitespace-trimming.
    
    $gen1 = range(1953,1962);
    
    $gen2 = range(1963,1967);
    
    if($name == "Corvette"){
        if(in_array($year,$gen1) === true){
            echo "Generation 1 Corvette";
        }
        elseif(in_array($year,$gen2) === true){
            echo "Generation 2 Corvette";
        }        
        else{
            echo "No Generations";
        }
    }
    
    PHP:

    Of course, using range() only works because the years are consecutive. If you have non-consecutive numbers or text that you wanted to compare with, you would just replace range() with array() like this:

    
    $gen1 = array(1951,1955,1962,1967);
    
    //or
    
    $gen1 = array("text1","text2","text3","text4");
    
    PHP:
     
    Louie55, Sep 3, 2012 IP