2 ways

This is a discussion on 2 ways within the PHP Language forums, part of the PHP Programming Forums category; hi all, Having a session variable test . Is there a difference between: $test = "string"; or $_session['test'] = "...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-27-2003
Stijn Goris
 
Posts: n/a
Default 2 ways

hi all,

Having a session variable test .

Is there a difference between:
$test = "string";
or
$_session['test'] = "string";

kind regards
Stijn


Reply With Quote
  #2 (permalink)  
Old 10-27-2003
Steve
 
Posts: n/a
Default Re: 2 ways

Yep first of all $_session doesn't exist

$_SESSION does though, so lets not forget about case sensitivity.
For portablity sake ALWAYS make sure you use
$_SESSION['test']="string";
Especially if you're not sure what version of PHP the host is running,
and/or what there php.ini reads like.
Also if register globals is on and there is a $_POST['test'] or a
$_GET['test'] you could get a conflicting variable that will be a HUGE
pain in the ass to track down.
Always best to test as far up the pipe as you can and then explicitly
assign.
I do it one of two ways, depending on what exactly I need.
I call this extremely explicit

if($_POST['test']){
$test = $_POST['test'];
$_SESSION['test']=$test;
}elseif($_GET['test']){
$test = $_GET['test'];
$_SESSION['test']=$test;
}elseif($_SESSION['test']){
$test = $test;
$_SESSION['test']=$test;
}

Or the sloppy way

extract($_SESSION);

If register globals (I think thats it) is turned on $_SESSION['test']
will automagically update, otherwise $_SESSION['test']=$test; is the
only way to go.

Someone please correct me, on the register globals comment, if I'm
wrong, but I know it's something.

Anyways more details can be had here
http://us2.php.net/manual/en/reserve...iables.session

"Stijn Goris" <mepisto@hotmail.com> wrote in message news:<3f9d05d2$0$297$ba620e4c@reader3.news.skynet. be>...
> hi all,
>
> Having a session variable test .
>
> Is there a difference between:
> $test = "string";
> or
> $_session['test'] = "string";
>
> kind regards
> Stijn

Reply With Quote
  #3 (permalink)  
Old 10-27-2003
Brian
 
Posts: n/a
Default Re: 2 ways

Uhm, this looks like assignment to me. A test would be conditional statment
like if.

if ( $_SESSION['test'] == 'bigfatbooger') {
do::someshit('bathroom');
}

= ( Assignment )
== ( test for equality )

There is also tests for existance, TRUE/FALSE tests.

if ($_SESSION['test']) {
cook::food('pizza');
} else {
pig::out('pork rinds');
}

Make sense?

Also, watch your case, no such monster as $_session, its $_SESSION.

"Stijn Goris" <mepisto@hotmail.com> wrote in message
news:3f9d05d2$0$297$ba620e4c@reader3.news.skynet.b e...
> hi all,
>
> Having a session variable test .
>
> Is there a difference between:
> $test = "string";
> or
> $_session['test'] = "string";
>
> kind regards
> Stijn
>
>



Reply With Quote
  #4 (permalink)  
Old 10-28-2003
Steve
 
Posts: n/a
Default Re: 2 ways

I think what he means is, does $_SESSION automagically keep track of
it's member variables and update accordingly.
The answer is, yes/no/maybe sometimes, it depends on the php version
and it's config.
The point I'm trying to make is this... Make sure assignment is ALWAYS
pointing to exactly what you want to assign.

Assuming $_SESSION['test'] ==1

$test = $_SESSION['test'];
$test ++;

Will produce one of two results.
Either $test will now == 2 and $_SESSION['test'] ==2
Or $test ==2 and $_SESSION['test'] ==1

It would be best, to have done this instead

$_SESSION['test'] ++;

This way we now know for sure it == 2

By the way, just ignore my first post, it was WAY too early in the
morning and I was kinda sleepy when I wrote it.


"Brian" <cpnmscg02 (@) sneakemail.com> wrote in message news:<zBbnb.41390$Tr4.85919@attbi_s03>...
> Uhm, this looks like assignment to me. A test would be conditional statment
> like if.
>
> if ( $_SESSION['test'] == 'bigfatbooger') {
> do::someshit('bathroom');
> }
>
> = ( Assignment )
> == ( test for equality )
>
> There is also tests for existance, TRUE/FALSE tests.
>
> if ($_SESSION['test']) {
> cook::food('pizza');
> } else {
> pig::out('pork rinds');
> }
>
> Make sense?
>
> Also, watch your case, no such monster as $_session, its $_SESSION.
>
> "Stijn Goris" <mepisto@hotmail.com> wrote in message
> news:3f9d05d2$0$297$ba620e4c@reader3.news.skynet.b e...
> > hi all,
> >
> > Having a session variable test .
> >
> > Is there a difference between:
> > $test = "string";
> > or
> > $_session['test'] = "string";
> >
> > kind regards
> > Stijn
> >
> >

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 06:43 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0