SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: PHP maths formular needed

  1. #1
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,030
    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 Advisor silver trophybronze trophy
    SitePoint Award Recipient cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    2,811
    Mentioned
    44 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

  3. #3
    Community Advisor silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    1,642
    Mentioned
    99 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.

  4. #4
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    1,863
    Mentioned
    28 Post(s)
    Tagged
    0 Thread(s)
    This whole situation just seems odd... if you ask me.
    <?php
    //Kyle Wolfe
    echo devBlog("My Dev Notes");

  5. #5
    Community Advisor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,141
    Mentioned
    34 Post(s)
    Tagged
    2 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
  •