If I gather data from my database, stick it into an Array, use that array to populate a Form, and then the Form is submitted, what happens to the data in my Array?
I know that $_POST persists across a Form being submitted, and I think that the data in my hypothetical Array above would be lost, but I'm not sure... :-/
Once the page has finished loading, PHP's garbage collection will come along and free up any allocated resources (from actions that have finished), which includes deleting your array data. So yes, your array data will be lost, but also saved into the POST data (upon form submission), which can be access through PHP's superglobal $_POST.
As a side note, if you need Arrays to persist across pages not using $_POST then serialize them and insert that serialized array into the database, then unseralize it on another page where you need it.
Read up on "variable scope". But for your question, you should realize that by the time the user sees the web page, PHP is done (not always but ignore output buffering for now). So the PHP makes some HTML and sends it to the user like mailing a letter. The user can send you a letter back by submitting the form. The letter your PHP gets back will have the $_POST variable filled. This letter is written in the user's handwriting and is completely separate from the one you sent.
We have sessions (and other methods) if you need to save some info from the letter you wrote and compare it to the one you get back but otherwise any thoughts you had while writing your letter (the variables you used) are gone and forgotten. Once you send your letter your mind is erased!
$_SESSION persists across page loads.
I am having a larger issue, and thought using an array might help.
It is probably better to stick with the $_POST array since that is what it is designed for, i.e. to pass data between scripts after a Form is submitted.
$POST and $GET are designed to send data from client to server. $_SESSION is designed for non-permanent, but persistent storage on the server-side during multiple page requests.
This topic is now closed. New replies are no longer allowed.