0

I am trying to complete an objective for a homework assignment and Im running into some issues. These were the instructions given to me:

Create a class named DaysofWeek. Include an array of the 7 days of the week as a member of the class. Include a function as a member of the class that when called, displays the contents of the array. Create an object and call the function.

My textbook is "w3schools" and im finding it a little difficult to understand everything fully to complete this task.

PHP

class DaysofWeek{
    var $days = array("Monday", "Tuesday", "Wednesday", "Thursday". "Friday");

    function showArray(){
        $arrlength = count($days);
        for ($x = 0, $x < $arrlength, $x++){
            echo $days[$x];
            echo "<br>";
        }
    }
}

This is the code I have written. The way I believe I have written is to the extent of:

I created a class called DaysofWeek. inside that class I defined a var $days which is an array of strings, each string being a day of the week. Then I have a function called showArray which in theory will for loop through the array display each day of the week. I create a variable called $arrlength to "count" the indexes of the array to give me the length of the array. Then I have a "for loop" that will loop through the array and echo each index of the array according to what number $x is. so if $x is 0 it should print "Monday"....

Two major issues I'm having:

I am getting this error: Parse error: syntax error, unexpected ')', expecting ';' in C:\xampp\htdocs\program-05\program05.php on line 71

line 71 is this: for ($x = 0, $x < $arrlength, $x++){

Second thing, even if I wasn't getting this error, I'm not certain my code is correct and complies with the instructions. I am the least bit to say confused about how classes work, even after reading several things. There's some syntax I've seen used in examples that I cant figure out what it does.......

what is the issue with my code?

kronis72
  • 303
  • 1
  • 4
  • 11

2 Answers2

3

($x = 0; $x < $arrlength; $x++) note the semi-colons

geoB
  • 4,578
  • 5
  • 37
  • 70
2

I have corrected some mistake. If you just want to print days using Class and Object, then try this. If you are looking for something else then let me know.

   <?php
      class DaysofWeek{
        function showArray(){
              $days=array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday");

              $arrlength = count($days);
              for ($x = 0; $x < $arrlength; $x++){
                  echo $days[$x];
                  echo "<br>";
              }
          }
      }

      //creating object
      $d=new DaysofWeek();
      // calling function
      $d->showArray();

      ?>
Blue Rose
  • 523
  • 1
  • 3
  • 14