简体   繁体   English

PHP在数组中使用常量

[英]PHP Use of constants in array

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. 我在学习PHP并尝试进入更高级的“内容”时,我的讲座建议我们最好的学习方法之一是使用/浏览/使用开源代码。

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? 为什么会出现错误,为什么在调用函数时Use of undefined constant week - assumed 'week' ,我可以采取哪些步骤进行更正?
  3. Why are the following variables declared as global global $weekStats, $playerTotals, $possibleScoreTotal; 为什么将以下变量声明为全局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(); 注意:为防止代码过长,我想在函数$lastCompletedWeek = getLastCompletedWeek();添加第4行$lastCompletedWeek = getLastCompletedWeek(); works correctly ie getLastCompletedWeek() returns correct result so correct result is passed to $lastCompletedWeek inside function. 可以正常工作,即getLastCompletedWeek()返回正确的结果,因此正确的结果将传递给$lastCompletedWeek内部函数。

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 该项目的链接可以在这里找到

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. 他们应该在引号中加上"week" ,但是他们没有,PHP帮了他们。

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? 2.为什么会出现错误使用未定义的恒定周-调用函数时假定为“周”,我可以采取哪些步骤进行更正?

Because there is no such constant in there code. 因为那里的代码中没有这样的常数。 See Answer 1 见答案1

3.Why are the following variables declared as global global $weekStats, $playerTotals, $possibleScoreTotal; 3,为什么将以下变量声明为全局全局$ 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" PHP将认为anotherGreeting是一个常量,它将尝试查找并提出警告,然后通过认为您的意思是"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. 当您收到未定义的索引错误时,它们表示您正在尝试访问数组中不存在的数组索引。

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 好吧,php解释器将读取诸如week类的常量作为常量,如果该常量在某处未定义,则php会发出此通知(这不是错误),要解决此问题,必须引用数组元素

Why are the following variables declared as global global $weekStats, $playerTotals, $possibleScoreTotal; 为什么将以下变量声明为全局全局$ 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 . 顺便说一句, 全局变量用于赋予变量全局访问权限并使其超出变量范围规则(可以这么说),这是一种不良的编程实践,因此不建议特别在OOP编程中使用。

define constant before function 在函数之前定义常量

    define('week','week');
    define('wins','wins');
    define('name','name');
    define('userName','userName');
    define('score','score');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM