SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,531
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Life of an Array?

    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...

    Thanks,


    Debbie

  2. #2
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Location
    United Kingdom
    Posts
    77
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    2 Thread(s)
    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.

    Steve
    ictus==""

  4. #4
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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!
    - Robert

  5. #5
    Barefoot on the Moon! silver trophy
    Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,524
    Mentioned
    51 Post(s)
    Tagged
    1 Thread(s)
    $_SESSION persists across page loads.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  6. #6
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,531
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Force Flow View Post
    $_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.


    Debbie

  7. #7
    Barefoot on the Moon! silver trophy
    Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,524
    Mentioned
    51 Post(s)
    Tagged
    1 Thread(s)
    $_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.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •