PHP Global variable

I trying to create a global PHP variable that I can use everywhere on a website but it doesn’t seem to work. My code looks like below.

                         $from= um_user('postal_zip_code');
                          global $from;


I’m trying to us it within the array below.

        // Our parameters
        $params = array(
            'origin'        => $from,
            'destination'    => $destination,
            'sensor'        => 'true',
            'units'            => 'imperial'

You can make variables global to your entire site by adding them to $_SESSION[]

Global variables are bad practices that you should not use in your application, it is a habit that you should get rid of before developing a serious web application. With global variables, you have no idea where it comes from and whether its state has been modified somewhere else. This can lead to unexpected behaviors, makes your code buggy, hard to test, and impossible to maintain. There are other side effects of global variables, such as slower performance, tighter coupling and namespace pollution. Read this article below to learn more about this, and a rule of thumb is to never use global variables:

Interesting. So no $_GET,$_POST,$_SESSION? Be curious to see how your php web apps work without globals.

Yeah you make a point, but with these superglobals you dont have a choice. I guess I should re-phrase myself that ‘you should not use global variables in your application, except for PHP’s superglobals with which you dont have an alternative’. Still, it is due to the limitation of PHP as a language that you have to use these superglobals, in Java, C# and Scala you dont have such problems anyway.

Also avoid the use of $_REQUEST if you’re getting data from a url query string ALWAYS use $_GET . If you’re getting data from a form and it’s not via using the get method, ALWAYS use $_POST

Maybe a silly question…

With a PHP generated page that calls JavaScript, is it possible to use the JavaScript to return a server PHP generated global variable?

If you get the JavaScript to create and submit a form then the server will receive $_POST variables.

If you do an ajax call from JavaScript you can send either $_GET orr $_POST variables.

1 Like

Is it a limitation or is it just the convenience of having an object that is already built and waiting for us, that is missing? If you ask me, it is the latter, which might actually mean added flexibility? Hmm…naw, I don’t think that. I actually wonder why PHP doesn’t just form a request object for us. Maybe it’s so the hardcore functional PHP programmers can keep their jobs! LOL! :smiley:

At any rate, from what I’ve learned, a good OOP PHP app will always take the PHP globals and add them to properties of a request object. This is actually defined in a relatively new PSR too. If PHP offered its own request object, this PSR wouldn’t really be necessary. Right?


Well the reason why PHP does not have a objects like request, response and session is that its superglobal arrays came a long way before PHP introduced OOP and OOPHP becomes mainstream. If you recall, PHP started off as a template language for C, and then evolved into a procedural language. Until PHP 5 it did not have a comprehensive set of object oriented features that make it a decent OO language. People who started with PHP 4 or even PHP 3 in the old days did not have the habit of using objects, even now the world of PHP still has many newbies who can only write amateurish procedural code.

In a way superglobal request arrays make it simpler for the very beginner to use and understand, as they get no exposure to OOP. In another way the advanced coders have built their own library for request object, like Symfony, Zend and other third party independent libraries such as Guzzle. Right now the PHP community does not have a standard of how to create, use and manipulate request/response objects. Many PHP core developers see this as a reason not to have a built-in HTTP request/response OO system, since people have all been so used to their own ways of doing this. In their minds, its better for the developers themselves to build their own OO abstraction layer for request/response, since its more flexible. I cant say I agree with this, but a lot of PHP internals actually believe this.

I can’t say I agree with it either. :smile: The PHP-FIG group at least agreed on a standard handling of the request message. See the PSR I linked to above.

But, as it seems (and as it always is), not everyone agrees with it and thus, PHP with its globals leaves the door open for people to do whatever they want to do. That could be seen as a blessing…or a curse.


I think his original question wanted a var that can be used on multiple pages, ie… home, about, contact etc.
Global variables are not bad practices, they are essential.

What you are looking for are the super globals $_COOKIE and/or $_SESSION.
(underscore goes after $ but discourse removes it)
You can set a cookie that can be read/used throughout the site.
Also you can start a session that can be used throughout the site, sessions are used mainly for login scripts.

just google for set cookie and start a session.

That’s my take on this.

No it doesn’t -$_SESSION and $_COOKIE display fine in discourse as long as you wrap them in backticks so that the underscore doesn’t get misinterpreted as a formatting instruction.

I can never remember that, I get it now $_POST['new_to_this']
Cool hope it helps the OP

And heres a very good video about global variables/states and how they are bad, the concept extends to singleton as well, which are none other than glorified globals:

1 Like

I’m a bit surprised it hasn’t been mentioned yet (maybe it’s off-topic?) but I think that in some cases using a CONSTANT is a better alternative than a Global.

Please, what approach can I use to achieve this?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.