Author Topic: off topic, but any answer appreciated  (Read 325 times)

Legacy_TrekSL

  • Newbie
  • *
  • Posts: 35
  • Karma: +0/-0
off topic, but any answer appreciated
« on: August 05, 2011, 03:42:18 pm »


               Our teacher gave us a days in month function in php, 99% of it i understand apart from the final element where he accesses the array. Due to a lack of info on this, could someone explain the last line of executed code to me - because it looks like we access the array according to the variable $month and then read -1 from that which makes bugger all sense to me!:

[/code][/code]function days_in_month($month=0,$year='')
{
if($month < 1 OR $month > 12)
    {
        Return 0;
    }
if(! is_numeric($year) OR strlen($year) !=4)
    {
         $year = date('Y');
    }
if($month == 2)
    {
    if($year %400 == 0 OR($year % 4 ==0 AND ($year % 100 !=0 ))
    {
        return 29;
    }
    }
$days_in_month=array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
return $days_in_month[$month - 1];
}
               
               

               


                     Modifié par TrekSL, 05 août 2011 - 02:44 .
                     
                  


            

Legacy_Roddish

  • Jr. Member
  • **
  • Posts: 82
  • Karma: +0/-0
off topic, but any answer appreciated
« Reply #1 on: August 05, 2011, 04:20:08 pm »


               i'm just guessing, but is it becuase most arrays start at position Zero rather than one, so instead of 1-12 months you have 0-11
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
off topic, but any answer appreciated
« Reply #2 on: August 05, 2011, 04:20:27 pm »


               Ok,  I do not know the language(PHP)   however I think I can answer this question for you.  

Your Array
$days_in_month=array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

Arrays are 0 based indexed.   that means you have an array with 12 elements in it that are indexed in the range of 0-11

so
$days_in_month(0) would = 31
and

$days_in_month(1) would = 28,

There is no index of 12 in your array.   it has 12 elements indexed 0 throuth 11

Since your month numbers are in the range of 1 -12, you need to subtract one from the month number to have it line up with the array.  

Hope that helps
L8
               
               

               
            

Legacy_Roddish

  • Jr. Member
  • **
  • Posts: 82
  • Karma: +0/-0
off topic, but any answer appreciated
« Reply #3 on: August 05, 2011, 04:25:14 pm »


               And the award for overzealous wordcount goes to Lightfoot8

'<img'>
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
off topic, but any answer appreciated
« Reply #4 on: August 05, 2011, 04:30:27 pm »


                'Image   I got an Award.    Cool.

Roddish wrote...

And the award for overzealous wordcount goes to Lightfoot8

'<img'>


               
               

               
            

Legacy_Roddish

  • Jr. Member
  • **
  • Posts: 82
  • Karma: +0/-0
off topic, but any answer appreciated
« Reply #5 on: August 05, 2011, 05:06:23 pm »


               I wouldn't get too excited. You should see it, I made it out of my earwax and some bubble gum i found under my chair.
               
               

               
            

Legacy_CID-78

  • Sr. Member
  • ****
  • Posts: 261
  • Karma: +0/-0
off topic, but any answer appreciated
« Reply #6 on: August 05, 2011, 05:12:54 pm »


               I can only confirm what been said. PHP is a bit easier since you only need to care if a variable is a single variable $ or array @. No need for types or memory size.
               
               

               
            

Legacy_TrekSL

  • Newbie
  • *
  • Posts: 35
  • Karma: +0/-0
off topic, but any answer appreciated
« Reply #7 on: August 06, 2011, 10:53:48 am »


               Crap! I forgot counting starts at 0. NOW that makes sense '<img'> Lightfoot and Roddish to the rescue '<img'>
               
               

               
            

Legacy_Roddish

  • Jr. Member
  • **
  • Posts: 82
  • Karma: +0/-0
off topic, but any answer appreciated
« Reply #8 on: August 18, 2011, 01:51:10 pm »


               crap!! does that mean i get an earwax award too?? knickers!!