1

Im learning PHP and trying to move on to a bit more advanced "stuff" my lecture advised us one of the best ways to learn is working / looking through / playing with opensource code.

Im currently inspecting an opensource project's code, and trying to slightly tweak the code.

The function is as follows:

 function calculateStats() {
    global $mysqli, $weekStats, $playerTotals, $possibleScoreTotal;
    //get latest week with all entered scores
    $lastCompletedWeek = getLastCompletedWeek();

    //loop through weeks
    for ($week = 1; $week <= $lastCompletedWeek; $week++) {
        //get array of games
        $games = array();
        $sql = "select * from schedule where weekNum = " . $week . " order by gameTime, gameID";
        $query = $mysqli->query($sql);
        while ($row = $query->fetch_assoc()) {
            $games[$row['gameID']]['gameID'] = $row['gameID'];
            $games[$row['gameID']]['homeID'] = $row['homeID'];
            $games[$row['gameID']]['awayID'] = $row['visitorID'];
            if ((int)$row['homeScore'] > (int)$row['visitorScore']) {
                $games[$row['gameID']]['winnerID'] = $row['homeID'];
            }
            if ((int)$row['visitorScore'] > (int)$row['homeScore']) {
                $games[$row['gameID']]['winnerID'] = $row['visitorID'];
            }
        }

            $playerPicks = array();
        $playerWeeklyTotals = array();
        $sql = "select p.userID, p.gameID, p.pickID, p.points, u.firstname, u.lastname, u.userName ";
        $sql .= "from picks p ";
        $sql .= "inner join users u on p.userID = u.userID ";
        $sql .= "inner join schedule s on p.gameID = s.gameID ";
        $sql .= "where s.weekNum = " . $week . " and u.userName <> 'admin' ";
        $sql .= "order by u.lastname, u.firstname, s.gameTime";
        $query = $mysqli->query($sql);
        while ($row = $query->fetch_assoc()) {
            $playerPicks[$row['userID'] . $row['gameID']] = $row['pickID'];
            $playerWeeklyTotals[$row['userID']][week] = $week;
            $playerTotals[$row['userID']][wins] += 0;
            $playerTotals[$row['userID']][name] = $row['firstname'] . ' ' . $row['lastname'];
            $playerTotals[$row['userID']][userName] = $row['userName'];
            if (!empty($games[$row['gameID']]['winnerID']) && $row['pickID'] == $games[$row['gameID']]['winnerID']) {
                //player has picked the winning team
                $playerWeeklyTotals[$row['userID']][score] += 1;
                $playerTotals[$row['userID']][score] += 1;
            } else {
                $playerWeeklyTotals[$row['userID']][score] += 0;
                $playerTotals[$row['userID']][score] += 0;
            }
        }
    }
}

echo calculateStats();

When I call the function, I get the following error message:

Use of undefined constant week - assumed 'week'

Use of undefined constant wins - assumed 'wins'

It gives the same error message for each index in the loop which does not contain '' (quotes)

When I do add '' (quotes) between the constants in the loop I get an undefined index error

Question

  1. Why are they using constants inside the loop on array variables?
  2. Why am I getting the error Use of undefined constant week - assumed 'week' when calling the function, and what steps can I take to correct it?
  3. Why are the following variables declared as global global $weekStats, $playerTotals, $possibleScoreTotal; ?

NOTE: to prevent the code from getting to long in the question I would like add line 4 inside the function $lastCompletedWeek = getLastCompletedWeek(); works correctly i.e. getLastCompletedWeek() returns correct result so correct result is passed to $lastCompletedWeek inside function.

I hope my question makes sense, if you need any more information please let me know, thank you very much!

The link of the project can be found here

Marilee
  • 1,598
  • 4
  • 22
  • 52
  • One reason may be: defining a constant, and if db col name changes, only one place to change the column name on this event. Second, is poor programming, because PHP treats undefined constant names as strings, hence you get the error. On the globals... no idea. Is there any trace of these variables any other place? – Balazs Vago Mar 07 '17 at 07:53

3 Answers3

1

They are not using any constants whatsoever.

Why are they using constants inside the loop on array variables?

They should have put "week" in quotes and they didn't and PHP helped them.

2.Why am I getting the error Use of undefined constant week - assumed 'week' when calling the function, and what steps can I take to correct it?

Because there is no such constant in there code. See Answer 1

3.Why are the following variables declared as global global $weekStats, $playerTotals, $possibleScoreTotal; ?

Poor programming practice. These variables should be passed over to the function.

To have a string index on an array you put the index in quotes

$array["greeting"]="hi";

If you have an already defined constant you don't need quotes

  define("greeting",  "hi");
  echo greeting;

But if you go ahead try

 $array[anotherGreeting]="Test";

PHP will think anotherGreeting is a constant which it will try to find and raise a warning and then it will try to help you by thinking you meant "anotherGreeting"

When you get undefined index errors they mean that you are trying to access an array index which is not already present in the array.

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
  • Thank you very much for your help, just one quick question if you dont mind, When I do add '' (quotes) between `week` and other indexes in the loop which doesn't contain quotes I get an undefined index error, any reason / suggestion as to why I would get this error? – Marilee Mar 07 '17 at 08:01
  • I should probably also add a link to the project https://github.com/rothkj1022/phppickem – Marilee Mar 07 '17 at 08:02
  • http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef – Hanky Panky Mar 07 '17 at 08:30
1

Question

Why are they using constants inside the loop on array variables?

  • well, it's hard to answer until we see the full project, but often this is a typo and they have to correct this by quoting it .

Why am I getting the error Use of undefined constant week - assumed 'week' when calling the function, and what steps can I take to correct it?

  • well, php interpreter will read some thing like week as a constant , and if that constant is undefined in somewhere php will produce this notice ( it's not an error ) ,to solve this you have to quoting the array elements

Why are the following variables declared as global global $weekStats, $playerTotals, $possibleScoreTotal; ?

  • global variables used to give the variable globally access and to make it beyond the variable scope rules -so to speak- , by the way , it's a poor programming practice , and it's not -highly- recommended to use specially in OOP programming .
hassan
  • 7,812
  • 2
  • 25
  • 36
0

define constant before function

    define('week','week');
    define('wins','wins');
    define('name','name');
    define('userName','userName');
    define('score','score');
Rafiqul Islam
  • 1,636
  • 1
  • 12
  • 25