Jump to content

numeric key in session array


niche

Recommended Posts

The keys in the $_SESSION associative array are subject to the same limitations as regular variable names in PHP, i.e. they cannot start with a number and must start with a letter or underscore. For more details see the section on variables in this manual.
From: http://www.php.net/manual/en/session.examples.basic.phpThe keys serve as identifiers, so it's really much better and easier to use descriptive text than a number.
Link to comment
Share on other sites

I'm really appreciative of your help. I also googled my question, but only found non-authortative info suggesting the the quote you found in the manual. I had plenty of reason to expect your info was there, but I just could'nt find it, so thanks again.Having said that (and part of my initial confussion) was what I found in the manual before I started this topic. What does the manual mean when it says: "A key may be either an integer or a string."?http://php.net/manual/en/language.types.array.phpIsn't an integer also a numeric?

Link to comment
Share on other sites

array can take both a integer "$array[1]" where 1 is reffering the index number.orcan take string $array['something'] where 'something' is reffering the key of the array. (associative)now session only take assocative array as $_POST or $_GETedit: associative array cant take numeric value. suppose you write$_SESSION['1']though it is quoted to make it as string value. but when it will evaluate it will reprsent same as $_SESSION[1]. and it is not possible with sessionand show up a php noticeNotice: Unknown: Skipping numeric key 1 in Unknown if i am not mistaking...numeric means integer in string represention you cant do (+,-,/,*). but you can do it with integer.though php can autodetect var type soa="1";b=2;c=a+b;echo c;c will show 3;but in other language it is not possible (eg c++). it must be same data type

Link to comment
Share on other sites

A normal array can have numeric indexes and associative indexes. The manual says so. I did a little experimenting with a "mixed" array, and it worked just fine. I mean, I created an array and populated it with associative indexes first. Then I added numeric indexes. No warning popped up.Only when I tried adding numeric indexes to $_SESSION did I get the "skipping warning." And it was only a warning. The elements were in fact added to the array. So I guess PHP treats $_SESSION a little differently. That should not be surprising.Since count() returns the total number of elements in an array, not just numeric elements, you can not use the return value of count as the limit of a for-loop. A for-loop initializer would have to look something like this:for ($i = 0; isset($_SESSION[$i]); ++$i) . . .(Actually, I haven't tested that, but it sounds correct.) I suppose there is some utility to that, but I'm not sure what.Still, I hate to run code that throws warnings.

Link to comment
Share on other sites

That's great news. I was working late last night and couldn't decide why I was having trouble with numeric values in session arrays. Turns turns out I wasn't gong crazy after all. Thanks for everyone's help especially wirehopper, birbal, and Deirdre's Dad.Niche

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...