Session variables problem
Posted: Wed Jul 14, 2010 8:21 am
Hello!
I've noticed this strange PHP behavior. Following example shows that these two variables, $_SESSION['name'] and $name are actually single variable. When one of them is given some value, the other takes this same value?!
This strange behavior had caused me many headaches in the past, until I have noticed it. I tried to google it but I don't know if PHP should behave like that or not?
Thank you!
I've noticed this strange PHP behavior. Following example shows that these two variables, $_SESSION['name'] and $name are actually single variable. When one of them is given some value, the other takes this same value?!
Code: Select all
session_start();
if (!session_is_registered('name')) session_register('name');
echo 'before:' . $_SESSION['name'];
$name = 'value 1';
echo '<br>after:' . $_SESSION['name']; // this should echo only 'after:' but it echoes 'after:value 1'
$_SESSION['name'] = 'value 2';
echo '<br>finally:' . $name; // this should echo 'finally:value 1' but it echoes 'finally:value 2'
Thank you!