FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » simple session question
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: simple session question [message #175759 is a reply to message #175696] Sun, 23 October 2011 15:47 Go to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma:
Senior Member
On Fri, 21 Oct 2011 20:19:53 +0200, Thomas Mlynarczyk wrote:

> You're missing a } here. But even with that and the session_start()
> issue fixed, it always prints 0. I'm not quite sure why

Look at the start of his code.

>> if(!isset($_SESSION['stp'])) {
>> session_start();
>> $_SESSION['stp'] = 0;
>> echo "session started<br>\n";
>> }

He always starts a session, and part of his session startup sets stp to 0.

So stp is always reset to 0 when a session is [re]started, not just when
a new session is created.

Now look at his "case 0" code

>> case 0: $_SESSION['stp'] = $_SESSION['stp']++;

The value of the expression "$_SESSION['stp']++" is the value of $_SESSION
['stp'] *before the increment operation*

eg, in:

x = y++;

x = original y
y = original y + 1

so he increments $_SESSION['stp'] and then assigns the value before it
was incremented back to the variable. As an aside, doing such things is
normally considered really really bad coding practice, as you can't
always guarantee that you'll get the same result, as you don't always
know which storage operation will occur last and thus determine the final
value of $_SESSION['stp']

e.g. the incremented value $_SESSION['stp']++ may be stored before or
after the evaluated expression $_SESSION['stp']= stores it's value. I
suspect that with c compilers, this would be an "undefined behaviour"
issue.

However, what's happening here is that by using a post increment, the
evaluation of the right side is the original value of the array element,
and that's the value that gets stored last.

So, he [re]starts the session, sets the 'stp' value to 0, increments it
and then over-writes the incremented value with the value before
incrementing, and finally:

>> echo "stp:".$_SESSION['stp'];

outputs the value, which is still 0.

And that, I suspect, is why he always gets a 0.

Basically, using:

x = x++;

is the headache, because in a single mathematical operation, you attempt
to define two conflicting values for x.

If he wants $_SESSION['stp'] to be 0 on the first pass and always
increment in the sequence 012012012 I'd do something like this:

session_start()
if(!isset($_SESSION['stp'])) {
$_SESSION['stp'] = 2;
echo "new session created<br>\n";
}
$_SESSION['stp'] = ($_SESSION['stp'] + 1) % 3;
echo "stp value is {$_SESSION['stp']}<br>\n";

I set the initial value to 2 so that the add 1, modulo 3 operation will
return a 0 on the first occasion after the variable is created. Setting
the initial value to -1 will also work. If the initial value is set to 0,
the value will be 1 after the increment, which may not be what the OP
expects the first time the page is loaded? Of course, this will be for
the OP to decide in the context of how he uses the stp value.

Rgds

Denis McMahon
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: session cookie: client side
Next Topic: by get this format my explode file name like this through php
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Dec 22 02:24:33 GMT 2024

Total time taken to generate the page: 0.04363 seconds