How long is a global stored? (PHP)

Hey Everyone,

I was looking through a script we have on the local environment and it contains a global.
What the script does is checking if the user is already in the $GLOBALS['cceLeadList']. If not it will be added to the Global. Else it would skip the script.

        $GLOBALS['cceLeadList'][] = $this->getLeadInfo($lead, 'email');
    }

    public function didAlreadyAddAttachement($leadEmail)
    {
        if (!isset($GLOBALS['cceLeadList'])) {
            $GLOBALS['cceLeadList'] = [];
        }

        if (array_search($leadEmail, $GLOBALS['cceLeadList'])) {
            return true;
        }

        return false;
    }

Now I am wondering where the global is stored. Let say the file is called multiple times. Would the global get emptied every time?

It lasts as long as any other variable that does not connect to a permanent storage.

$GLOBALS is not a session storage.

If you’re trying to persist variables between page loads, then you’ll need to start and maintain a session.

A global variable is only available for the duration of the script execution. However, global variables can also be populated from persistent data sources like mysql. That has nothing to do with their lifetime from a fundamental standpoint.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.