SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: Arrays

  1. #1
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrays

    I am having some trouble with arrays. I do not think my arrays are initializing. I an getting zero for sumx. Could someone take a look?

    PHP Code:
    class class1 {
         function 
    SUM(){
              
    $this->= array(0,1,2,3,4,5,6);
              for (
    $this->0$this-><= 6$this->k++){
                   
    $this->sumx += $this->x[k];
              }
         }


  2. #2
    Non-Member
    Join Date
    Jun 2012
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    your variable(x,k,sumx) not declared

  3. #3
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Are you expecting a return from the function SUM()? If so there is no return command. (return $this->sumx; )

    Also for your code, foreach is a better alternative as it's easier to read/understand. Or you can use a predefined php function (array_sum), though I assume you're just doing this for learning.

    Another thing, don't use one letter variables like x and k together -_-, very hard to read and easily confusing.

  4. #4
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    This line:
    $this->sumx += $this->x[k];

    should be this:
    $this->sumx += $this->x[$this->k];

    or update your code to this:
    PHP Code:
    <?php
       
    class class1 {
           function 
    SUM(){
               
    $this->= array(0,1,2,3,4,5,6);
               for (
    $k 0$k <= 6$k++){
                   
    $this->sumx += $this->x[$k];
               }
               return 
    $this->sumx;
           }
       }
       
    $sum = new class1();
       
    $total $sum->SUM();
       echo 
    $total;
    ?>

  5. #5
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Corrected the $this->k but still does not work.

  6. #6
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Works fine when I ran tom8's code.

  7. #7
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,081
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by b3hnam View Post
    your variable(x,k,sumx) not declared
    You don't have to declare variables in PHP; it's not Java
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


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
  •