Numeric values

Discussion in 'PHP' started by baris22, Feb 20, 2011.

  1. #1
    hello all

    I am trying to learn php. How can i make this if statement shorter.


    I am trying to make if statement that if $thisyear is less or more than $year by 1 or 2 or 3 than do something.




    
    <?
    
    $thisyear =  date("Y");
    
    $year = $_REQUEST['year'];
    
    ?>
    <select name="year">
    <?
    if ($thisyear != $year && $thisyear != $year - 1 && $thisyear != $year - 2 && $thisyear != $year - 3 && $thisyear != $year + 1 && $thisyear != $year + 2 && $thisyear != $year + 3) {
    ?>
    <option title="<? echo $thisyear; ?>"><? echo $thisyear; ?></option>
    <?
    } 
    ?>
      
      <option title="<? echo $year -3; ?>"><? echo $year -3; ?></option>
      <option title="<? echo $year -2; ?>"><? echo $year -2; ?></option>
      <option title="<? echo $year -1; ?>"><? echo $year -1; ?></option>
      <option selected value="<? echo $year; ?>"><? echo $year; ?></option>
      <option title="<? echo $year +1; ?>"><? echo $year +1; ?></option>
      <option title="<? echo $year +2; ?>"><? echo $year +2; ?></option>
      <option title="<? echo $year +3; ?>"><? echo $year +3; ?></option>
    </select>
    
    
    PHP:
     
    Last edited: Feb 20, 2011
    baris22, Feb 20, 2011 IP
  2. Simple Link Media

    Simple Link Media Peon

    Messages:
    38
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    if ($year <= $thisyear + 3) {
    
        // Do something
    
    } else {
    
        // Skip
    
    }
    PHP:
     
    Simple Link Media, Feb 20, 2011 IP
  3. ap2010

    ap2010 Guest

    Messages:
    41
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    if ( abs($thisyear - $year) <= 3 ) {
    
    echo "$thisyear = $year +/- 3";
    
    }
    PHP:
     
    ap2010, Feb 21, 2011 IP