Somk
Somk

Reputation: 12047

Use variable as SESSION Key

I am trying to save a session as either 1 or 0 and setup its key via another variable. Although when I go to use the SESSION data, it is not there. I am using the below code and would appreciate any advice.

session_start();
$num ='1';
            $_SESSION[$num] =='0';          
            if(isset($_SESSION[$num])){
                echo 'ran';
            }

Upvotes: 0

Views: 433

Answers (3)

JohannesAndersson
JohannesAndersson

Reputation: 4630

Only use one equal sign = not two ==

$_SESSION[$num] = '0';

Upvotes: 0

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

 $_SESSION[$num] =='0';   

this should be

 $_SESSION[$num] ='0';

= is used for assignment and == used for comparison

Upvotes: 4

Waleed Khan
Waleed Khan

Reputation: 11467

This is comparison, not assignment:

$_SESSION[$num] =='0';

Use a single equals sign:

$_SESSION[$num] = '0';

Upvotes: 4

Related Questions