• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 416
  • Last Modified:

Define Global variables in PHP

Hi,

I am completely new in PHP.
And want to create the global variable for my web application scripts that can be accessed anywhere in web application.
It should have less overhead in terms of memory
0
badalpatel
Asked:
badalpatel
  • 3
3 Solutions
 
Ray PaseurCommented:
For each page, use the array $GLOBALS and store your data there.

For inter-page communication use the $_SESSION array.
0
 
Ray PaseurCommented:
Also, if you have even a little programming experience and want to get a foundation in PHP, get this book and work thru the examples.  Excellent learning materials!
http://www.sitepoint.com/books/phpmysql4/

Best regards, ~Ray
0
 
Beverley PortlockCommented:
"...It should have less overhead in terms of memory"

You should not even be thinking like this unless you are going to be moving huge amounts of data around in memory. Remember that unlike BASIC or VB, typical PHP programs execute for only a fraction of a second and release their memory at the end of their run. For most PHP code, it executes between the click of the mouse and the first appearence of HTML on your screen. Resources like memory only get a very brief utilisation.

Global variables are a mess in PHP and will produce very poor quality program code. You will find all sorts of little inconsistencies and scope problems that you do not expect as the PHP scope rules are not like those of other languages. I have old PHP code with globals in it and they are utterly useless in terms of code re-use. They just lie around tied to systems they built in to and impossible to change. any substantial changes to the scripts can knock them out completely.

If you are new to PHP learn the safe way to code it. There is enough potential dangers in PHP without inventing more hazards to negotiate around.
0
 
Ray PaseurCommented:
Let me also echo bportlock's wise advice about good coding practices.  PHP5 has a fairly useful OOP structure.  However as I try to read between the lines of the question, I am thinking that the question is about passing data from page to page of the same web site.  The session handler is a good place to start learning about how to do this.  After that, learning about cookies and data bases would probably be a good next step.
http://us.php.net/manual/en/ref.session.php

Best of luck with your project, and please post back if you have any specific questions.  ~Ray
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now