Working with PHP in any situation can be daunting, especially in the beginning when all the code looks foreign to you. So I’m going to provide a very bare-bones, “human” definition of the PHP keyword, “global”.

First you should at least understand that PHP is organized by using functions, which looks like this:

function my_first_function() {
//Tell PHP to do something here

As you will soon learn, all functions begin with the word “function”, immediately followed by a name that you create – in this case, “my_first_function“. Inside of this function is where you tell PHP what you want it to do. And here you are allowed to create your own variables inside of these functions that help you get stuff done. For example, you may create a variable that looks like $var (the name of this variable is called “var”). The cool part is that with these “variables” you are allowed to define them in any way that you want. For example, you could set the variable equal to the number “2” or a set of words like, “I am a great programmer”. And you can obviously see that all variable names are preceded with the “$” symbol. Hopefully that is easy to understand.

The 2nd thing you should know is that by default, you are only allowed to use these variables inside of the function that you create. So for example, if you created another function called my_second_function(), and you wanted to use the variable you created previously (called $var, your second function would not understand the definition that you set for it.

Enter the global keyword in PHP. Here’s how you would use it in order to make that variable available in your 2nd function:

function my_second_function() {
global $var;
//Tell PHP what you want it to do here

There you have it – your first variable, $var is now available in your 2nd function my_second_function(). I know it’s a cheesy blog post, but hopefully it’s of help to someone.

Leave a Reply