How long does a variable last?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Fusion
Forum Newbie
Posts: 13
Joined: Wed May 12, 2004 9:48 am

How long does a variable last?

Post by Fusion »

The title explains it all. Does it just last untill the php is closed? Or untill it is assigned another value? I would like to make it so that in the header, it sets variables, but how do I make sure that it stays throughout the wholepage? Should I use a session? and if yes, how should I do it? make it so the seasson ends when the client goes to another page?

Thanks for your responce!
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

A variable will exist until one of two things happens:

1) You destroy it (usually via unset)
2) Your script terminates.

If you want to pass it from PAGE to PAGE then you need to consider using a session or serialised object.
dave420
Forum Contributor
Posts: 106
Joined: Tue Feb 17, 2004 8:03 am

Post by dave420 »

or shared memory, or a database, or a flat file, or any number of ways :)
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

True.. except all of those things require that you've maintained state between the pages in order to be able to extract the stored variable again.. ;)
dave420
Forum Contributor
Posts: 106
Joined: Tue Feb 17, 2004 8:03 am

Post by dave420 »

Very true :)
Post Reply