SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,048
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP maths formular needed

    Hi Guys!

    I need some kind of formular for PHP to use in an if statement. The if statement if written out, should work like so:

    PHP Code:
    if($number >= && $number <= 3)
    {

    }
    if(
    $number >= 12 && $number <= 15)
    {

    }
    if(
    $number >= 24 && $number <= 27)
    {

    }
    if(
    $number >= 36 && $number <= 39)
    {


    Is there a better way to write this?

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,050
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Couple of questions.
    1. Is the logic within each if block going to be different? I assume it would be otherwise, you could make one massive if statement.
    2. Is this the full set of logic, or is there more? As right now, it seems you want to check if $number is one of 4 digits, add 9, check again, etc. This is predictable, so it could be written in various ways
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  3. #3
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,326
    Mentioned
    191 Post(s)
    Tagged
    4 Thread(s)
    You can use the `case` (switch) statement for this.
    PHP Code:
    switch ($number) {
      case 
    0:
      case 
    1:
      case 
    2:
      case 
    3:
        
    //do something
        
    break;
      case 
    12:
      case 
    13:
      case 
    14:
      case 
    15:
        
    // do something else
        
    break;
      case 
    24:
      case 
    25:
      case 
    26:
      case 
    27:
        
    //do something else more
        
    break;
      case 
    36:
      case 
    37:
      case 
    38:
      case 
    39:
        
    // do yet another something
        
    break;
      default: 
    //NEVER, EVER, EVER forget the 'default'
        // default action - display an error???

    Not quite as elegant as it is in other languages, though.
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  4. #4
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,176
    Mentioned
    63 Post(s)
    Tagged
    2 Thread(s)
    This whole situation just seems odd... if you ask me.

  5. #5
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,797
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Try this:

    PHP Code:

      $a
    =array();
      for(
    $i2=0$i2<=3$i2++)
        
    $a array_merge$arange($i2*123+$i2*12) );
      
      
    $number 25
      echo 
    'Testing: '$number'<br />';
      
      if( 
    in_array($number$a) )
        if(
    $number 4)  
          echo 
    'Yes 0, 1, 2 or 3';

        else if(
    $number 16)
          echo 
    'Yes 12,13,14 or 15';

        else if(
    $number 28)
          echo 
    'Yes 24,25,26 or 27';

        else 
    # if($number < 40)
          
    echo 'Yes 36,37,38 or 39'
    Last edited by John_Betong; Oct 17, 2012 at 20:55. Reason: formatting


Bookmarks

Posting Permissions

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