Variable Scope: A Beginners Guide (PHP & Javascript)
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
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:
1 2 3 4 5 6 7 |
$a = 'info a'; function test() { var_dump($a); } test(); |
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:
1 2 3 4 5 6 7 8 |
$a = 'info a'; function test() { global $a; var_dump($a); } test(); |
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:
1 2 3 4 5 |
function test() { $a = 'info a'; } var_dump($a); |
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.
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:
1 2 3 4 5 6 7 |
x = 5 function test() { alert(x); } test(); |
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”> code here </pre> to post code. Also spam & abusive comments will be deleted.