Variable Scope: A Beginners Guide (PHP & Javascript)

/ Javascript, PHP / by Paul Robinson / 0 comments
This post was published back on July 21, 2009 and may be outdated. Please use caution when following older tutorials or using older code. After reading be sure to check for newer procedures or updates to code.

Variable scope is something that comes in handy in most coding languages, in my experience though it is invaluble in Javascript. If you are sitting thinking, ‘but what the hell is it’, then let me explain. Variable scope is a set of rules applied to a variable telling it where & it what context it may be accessed. An easier explanation is to think of a box. In the box is the information held by the variable, but written on the side of the box is a set of rules telling you where you have to be to open the box. It’s complicated, but it get’s easier with practice.


PHP has a slightly different method of using variable scope to that of Javascript, but since the two languages are generally used hand in hand I thought I’d cover them both.

PHP’s scope is actually quite simple. Look at this code example:

This code with produce the result NULL. Why? Well because the variable $a was created outside of the function test scope. Now if we change the code to this:

You will get the result string(6) "info a". This is because we have told the function to extend it’s scope & check globally for a variable called $a. Of course instead of using global you can always hand over variables in the form of parameters, however that creates a copy of the variable & does not alter the original unless it is passed by reference.

It is important to note that the opposite is also true. Meaning variables created inside the function cannot be accessed outside, for example:

That would again produce the result NULL. Of course the correct way would be to return the result from the function, however the previous code snippet was meant as an example of how scope works.

I think that should cover most of the beginner parts needed to understand PHP variable scope. Now onto Javascript.


Ok. Please be aware I’m still learning Javascript (in particular jQuery) and haven’t been using it for a long time, but I think I can give a fairly good description of Javascript’s variable scope.

It is similar in many ways to PHP, except for one major difference. While it is the same in that variables created inside functions cannot be accessed outside of the function (unless returned), it is different because variables created outside can be accessed inside functions. For example:

This would return the value 5 in an alert box.

Like PHP, Javascript also has an OOP system which involves variable scope, but in my opinion that is not for a beginners tutorial.

Anyway I think that’s about it. If I’ve missed anything or you have any tips please feel free to add them in the comments. WordPress is being a pain for me about code in the comments, but please try and use <pre lang=”php”&gt code here </pre> to post code. Also spam & abusive comments will be deleted.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

I'll keep your WordPress site up-to-date and working to its best.

Find out more