A Question of Scope |
Page 1 of 2 PHP is sometimes a strange language when viewed from the perspective of other languages. This PHP puzzle demonstrates that you can't expect every language to work in the same way.
BackgroundTo be honest right at the very start this is a puzzle that will really only puzzle the PHP programmer who is really grounded in another language. It is based on the fact that PHP does a very standard thing in what programmers from other languages could regard as the "wrong way round" - see if you agree. This puzzle is based on the idea of "scope". In early languages programmers would declare a variable and expect it to be available throughout their program. In today's jargon all variables were global. This was OK as long as programs were small but as they got bigger and especially when they got so big that they were divided into separate modules a common problem was name collision i.e. trying to give the same name to two variables. Name collisions still occur today - JavaScript, unless you take special care, is particularly prone to it. A partial solution to the problem is to introduce the idea of scope. A variable's scope is the "area" in a program where it can be used - i.e. where it can be accessed. Notice that this is a very odd definition because of the word "area". Basically the area where a variable is in scope includes anywhere you can write an instruction that works with that variable and not with some other variable that might just happen to have the same name. So in this sense a global variable, i.e. on that has global scope, can be used anywhere in the program and a variable that is local to a function i.e. on that has local scope, can only be used within that function. If there are two variables in scope then the one with the most restrictive scope is used. So if there are two variables with the same name and one has local scope and on global scope then within the function that it is defined the local variable is used. This is more or less how it is done in most languages but there are lots of variations on this basic theme. PuzzleYou have to remember that the code in this puzzle is stripped down to its basics to make sure you can see what is going on. It would be easy to make the puzzle seem more puzzling by simply obfuscating the code but where would be the fun in that? The essence of the problem is that a novice PHP programmer started out with some code: $MyGlobal="This is a global variable"; Where the form of the code is suppose to indicate that there was a variable that the programmer realised was needed by the whole of the program i.e. $MyGlobal and another i.e. $MyOtherVariable that really was only of use to a small part of the program. The two echo instructions are standing in for something complicated involving the global and the other variable. Of course the program simply displays the contents of the two variables and so you see This is a global variable After working with the program for a while it became clear that some refactoring was called for. The echo statements needed to be in a function and $MyOtherVariable was best implemented as a local variable. The refactoring was just: $MyGlobal="This is a global variable"; When this program is run it simply displays: This is some data the text in $MyGlobal has simply vanished. Where has $MyGlobal gone? Turn to the next page when you are ready to find out.
<ASIN:0470527587> <ASIN:1430210117> <ASIN:0596804377> <ASIN:0321442490> <ASIN:1590598628> |