What data can be accessed through the $_SESSION superglobal

120 Views Asked by At

I had this question in a quiz: Once a session is started, all data related to the session is accessible through the $_SESSION superglobal. A. True B. False

My answer was false, because I thought only the data stored explicitly like this: $_SESSION["something"] = "something", is accessible. But I the correct answer was true.

So, what else can I get from this superglobal? Or maybe it's just the wording issues, the actual question was asking everything in this session stored like this can be accessible?

1

There are 1 best solutions below

0
Tural Rzaxanov On

$_SESSION superglobal may be to use only with session. When you pass data with session also, you can get data with it. Data types can be string, int,float,array etc.

For example:

<?php
    // Start new or resume existing session.
    session_start();
    
    // Add values to the session.
    $_SESSION['item_name'] = 'value'; // string
    $_SESSION['item_name'] = 0; // int
    $_SESSION['item_name'] = 0.0; // float

    // Get session values.
    $value = $_SESSION['item_name'];