I haven’t got any data to benchmark this, so beforehand i’d like to know. What would you say would perform better with PHP with multiple users loading the same page?
1.100 sessions on a page each containing an array which each are unset at the end of the code 2.Multidimensional array: an array with 100 keys each containing an array.
overview
From reading your question I get the feeling that you don’t understand what sessions are and how they work.
Just in case, I’m going to explicitly state a few things about sessions that you may or may not know.
need
Sessions are typically used to contain temporary, persistent, user-specific data.
$_SESSIONis an array, so you can store as many key-value pairs as you’d like with multidimensional arrays or objects.The idea behind a session is to store all related data in a single session. If there are two sets of data that have independent state durations, then you should use two sessions.
Behind the scenes, a session is doing file IO, so there will be a necessary overhead in using a session.
performance
as
$_SESSIONisn’t all that different from any other array, there isn’t going to be a significant difference in code between using a single multidimensional array, and using 100 arrays. There will be a significant difference in the amount of read-write time used to open/close the sessions. If there’s no reason to have the data in separate sessions, use one session and alias the data as needed:Any arrays you create in global scope are automatically going to be multidimensional arrays anyway due to the
$GLOBALSarray (not that you should access them from there).tl; dr
Don’t use more than one session unless you have multiple data sets that need to persist separate from each other.
No premature optimization. Test instead of guess.