Re: Re: change value of session variable?
| From: | Five | Date: | Thu, 22 Jul 2004 04:48:16 +0000 |
| Subject: | Re: Re: change value of session variable? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-191472@lists.php.net to get a copy of this message | ||
"Chris Shiflett" <shiflett@php.net> wrote in message news:20040722030944.41256.qmail@web52801.mail.yahoo.com...
> --- Five <primeooze@hotmail.com> wrote:
> > My problem isn't the logic of when and where to output variable
> > values. It's figuring out when a session variable will accept
> > initialization and what enables and/or prevents it from doing so.
>
> There's no magic. Session variables behave exactly like any other
> variable. If you output a session variable before initializing it, you
> will see nothing (and a notice is generated, depending on your
> error_reporting setting).
>
> The only difference, in terms of using session variables, is that they
> persist from page to page.
>
----------------------------------------------------
page1.php
<?php
session_start();
echo 'page #1<br>';
echo $_SESSION['favcolor'];
$_SESSION['favcolor'] = 'green';
echo '<br><a href="page2.php">page 2</a>';
?>
----------------------------------------------------
page2.php
<?php
session_start();
echo 'page #2<br>';
echo $_SESSION['favcolor'];
$_SESSION['favcolor'] = 'blue';
echo '<br><a href="page1.php">page 1</a>';
?>
----------------------------------------------------
When I tested the above example, no matter how many times I clicked back and forth between the two
pages, the session variable would
not accept assignment of values 'blue' or 'green', while there should be no
value on the first look at page1 but should have echoed
'green' on the first visit to page2 as that was the value it was assigned on page1.
When I changed the initialization and assignment of the session variable to before attempting to
echo it's value, it then worked.
That started me thinking that placement of the initialization/assignment might make a difference on
whether a session variable would
accept assignment of a value. An ordinary variable would have accepted assignment of a value after
an attempt to echo it's value.