PHP: 使用全局时变量被重新定义?

PHP: Variables get redefined when using global?

我有一个带有一些变量的用户会话:

if(!isset($_SESSION)) {
    session_name('User Session'); 
    session_start(); 
}

$private_id = session_id(); 
$private_counter;
$private_max;
$private_startTime;

session_write_close(); 

现在我调用一个 init 函数来初始化这些变量:

function init($counter, $max, $startTime) {
    global $private_counter;
    global $private_max;
    global $private_startTime;

    $private_counter = $counter;
    $private_max = $max;
    $private_startTime = $startTime;

    $data = array(
        "counter" => $private_counter, 
        "max" => $private_max,
        "startTime" => $private_startTime
    );

    echo json_encode($data);
}

这 returns 例如以下内容:

counter: 12
max: 20
startTime: 1437309883114

所以我认为现在已经设置了变量,但是在我稍后调用的另一种方法中,我执行了以下操作:

function diff($endTime) {
    global $private_startTime;

    $diffTime = $endTime - $private_startTime;

    $data = array(
        "time" => $diffTime
    );

    echo json_encode($data);
 }

现在 $diffTime 始终是 $endTime,因为 $private_startTime 为空。但为什么它为空?我使用函数 init($counter, $max, $startTime)1437309883114 初始化了变量。当我使用 global 语句时会发生什么事吗?

如果不允许我使用 global,我还能如何在函数内访问我的用户变量?

编辑

完整的PHP-文件:

<?PHP

error_reporting(E_ALL);
ini_set('display_errors', 1);

date_default_timezone_set("Europe/Berlin");

if(isset($_POST["action"]) && !empty($_POST["action"])) {
    $action = $_POST["action"];
    $startTime;
    $endTime;

    if(isset($_POST["startTime"]) && !empty($_POST["startTime"])) {
        $startTime = $_POST["startTime"];
    }

    if(isset($_POST["endTime"]) && !empty($_POST["endTime"])) {
        $endTime = $_POST["endTime"];
    }

    switch($action) {
        case "init" : init($startTime); break;
        case "check" : check($endTime); break;
    }
}

if(!isset($_SESSION)) {
    session_name('User Session'); 
    session_start(); 
}

$private_id = session_id(); 
$private_counter;
$private_max;
$private_startTime;

session_write_close(); 

/**
 *
 *
 */
function init($startTime) {
    global $private_counter;
    global $private_max;
    global $private_startTime;

    $private_counter = 1;
    $private_max = 15;
    $private_startTime = $startTime;

    $data = array(
        "counter" => $private_counter, 
        "max" => $private_max,
        "startTime" => $private_startTime,
    );

    echo json_encode($data);
}

/**
 *
 *
 */
function check($endTime) {
    global $private_startTime;

    $diffTime = $endTime - $private_startTime;

    $data = array(
        "time" => $diffTime
    );

    echo json_encode($data);
}

?>

和 JavaScript-文件:

var $ = jQuery;

function init() {
    $.ajax({
        url: "./myFolder/user.php",
        data: {
            action: "init",
            startTime: new Date().getTime()
        },
        type: "post",
        success: function (output) {
            var data = $.parseJSON(output);
            var counter = data.counter;
            var max = data.max;
            var startTime = data.startTime;

            console.log("StartTime: " + startTime);
        }
    });
}

function check() {
    $.ajax({
        url: "./myFolder/user.php",
        data: {
            action: "check",
            endTime: new Date().getTime()
        },
        type: "post",
        success: function (output) {
            var data = $.parseJSON(output);
            var time = data.time;

            console.log("Time: " + time);
        }
    });
}

这就是我所做的,$private_startTime 在 php check($endTime) 函数中始终为 null。

我的用户会话工作正常吗?因为如果我这样做:

global $private_id;

并在我的 json 数据中返回它,它也是空的。

不,它们不会被重新定义。 运行这个测试代码,你会看到:

<?php
function diff() {
    global $private_startTime;
    var_dump($private_startTime);
}
function init($startTime) {
    global $private_startTime;
    $private_startTime = $startTime;
    var_dump($private_startTime);
}
$private_startTime = 0;
init(1437309883114);
diff();
var_dump($private_startTime);
?>

它打印了 3 次 1437309883114。问题出在您的代码中,我们看不到。 global 关键字不影响变量的值。来自 documentation:

By declaring $a and $b global within the function, all references to either variable will refer to the global version.

您可以直接使用$GLOBALS。 PHP $GLOBALS数组。

全局变量不会跨请求持续存在,您应该为此使用会话,并且使用 $_SESSION:

访问和修改会话变量
function init($startTime) 
{
    $_SESSION['private_counter'] = 1;
    $_SESSION['private_max'] = 15;
    $_SESSION['private_startTime'] = $startTime;

    $data = array(
        "counter" => $_SESSION['private_counter'], 
        "max" => $_SESSION['private_max'],
        "startTime" => $_SESSION['private_startTime'],
    );

    echo json_encode($data);
}

function check($endTime) 
{
    $diffTime = $endTime - $_SESSION['private_startTime'];

    $data = array(
        "time" => $diffTime
    );

    echo json_encode($data);
}