Global variable for use in multiple functions

I would really appreciate some ideas on the best way to achieve this result if possible guys.

I have a product page which I would like to dynamically generate using a number of functions inside one file. I would prefer to have one or two queries to grab the values I need from a MYSQL database, store them into a variable and then use those variable inside the various functions. I realise that maybe OOP is the obvious answer here but I’m not started down that road yet. Here’s my idea of doing this;

Product needs to show product-info, related products and an option to buy.

Create a main function that gets the data.
This data is then stored into global variables and then fed through to each function that requires them to run the function.

A function to get the product info, another function to get related products and another function to handle the cart-info all from the initial data function.

Would this be a viable solution or is this a potential security risk? I will using unique variable names that are only used inside a function.

At present I have some global variables for some functions and multiple queries getting data. Just need to know if this kind of set up may be faster.

In general it’s a good idea to stay away from global variables. You can always pass scoped variables to various functions to pass the info around if you need.


function getProductData($proId) {
    // Do some stuff
    return array('id' => $proId, 'desc' => $prodDesc);
function getRelatedProducts($dataArray) {
    //Do some stuff
$productData = getProductData(12);
$relatedProducts = getRelatedProducts($productData);

// etc...

That’s overly simplified of course, but it can help get you thinking on the right track.

Sounds like you have the right idea in the context of procedural programming though I would avoid globals like the plague. If you have php 5.3 on your server than I would recommend using namespaces to better organize the functions and keep them out of the global namespace as well.

Cheers guys for the advice. I’m going to look into both suggestions. Do any of you have any recommendations of tutorials to explain scoped variables or any examples of how this works and how I can use it to get what I need to do?

A great place to start for most things is the PHP manual. Here’s the section on variable scope:

If you want to know more, the comments in the manual are also a good place to look… as is Google.

Could I ask a few other questions regarding functions? Is it better to have all functions together in one file or separate in individual files? I would also like to know if because a global variable will only be recognised inside the function that creates it, how can this variable be accessed or hacked. I guess you need to know how to hack a variable before you can guard against it’s security. Apart from not using global variables is there any safeguards to securing them within PHP?

Thanks for all the responses so far guys.

Hacking PHP variables…that is a new one. >.>

Ok, maybe not hacking variables but just need to know what to safeguard against when dealing (if I have to) with global variables. Sorry if the expression raised a few eyebrows…

Well it depends how derpy you can be and how much protection you need to protect against yourself…

Mostly need simple sanity checks, expecting an integer? Check. Expecting…whatever, check it, etc. Thats pretty much it. For user submitted data*, validation and filtering.

  • Never trust user submitted data.

Well, there will be no user submitted data at all within these functions. The data is purely from a MYSQL database. Plus, the variables will be solely kept for these functions and not used throughout the site. Based on this, is it safe (within reason of course) to use several global variables within these functions to display data?

Thanks for the replies by the way (where’s my manners?..)