简体   繁体   中英

PHP variable in session variable

I am trying to use a session variable like this:

$string = 'abc';    
$_SESSION[$string];

But I get access if i use it like this:

$_SESSION['abc'];

But I get always an error like this:

Notice: Undefined index: abc

Any ideas to solve my problem? :/

Simply calling

$string = 'abc';    
$_SESSION[$string];

is not enough. It's only becomes available when you assign some value to it.

$string = 'abc';    
$_SESSION[$string] = 'test';

echo $_SESSION['abc']; //test

Also, make sure that session_start() is called on a page you're accessing it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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