Define Global variables in PHP

Posted on 2009-12-23
Last Modified: 2012-05-08

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
Question by:badalpatel
    LVL 107

    Accepted Solution

    For each page, use the array $GLOBALS and store your data there.

    For inter-page communication use the $_SESSION array.
    LVL 107

    Expert Comment

    by:Ray Paseur
    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!

    Best regards, ~Ray
    LVL 15

    Assisted Solution

    LVL 34

    Assisted Solution

    by:Beverley Portlock
    "...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.
    LVL 107

    Expert Comment

    by:Ray Paseur
    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.

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

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now