1

I am just a little confused about this situation.

    while($row = $stmt->fetch(PDO::FETCH_ASSOC)) 
    {
        $un = $row['USERNAME'];
        $pw = $row['PASSWRD'];
        $at = $row['ACCOUNT_TYPE'];
        $GLOBALS['fn'] = $row['FNAME'];
    }

So this code is inside a function. The variables $un, $pw and $at are all declared and given a value inside this block of code.

Now my understanding is that variables that are declared in a block of code are only able to be used in that block.

As you can see I also have a $GLOBALS['fn'] variable setup which I use in an other file so that makes sense to me to make it global.

Now my question is:

How is it possible to reference to a variable outside of a code block when it is declared inside a block?

According to this article here PHP Variable scope the variables declared outside of a function are not the same as inside the function even if they share the same name. For this I would need to prepend the variables inside the block with global or an $GLOBALS array.

NOTE to the duplicate - this may have been asked before I do not really doubt that since it is so simple. I just related it to my situation in my own words. Helps me understand it better. Also the question where this links to explains what exactly what I read in the linked article. It does however not address the while loop issue I was referring to. So in that case it is a bit different I believe.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
jwknz
  • 6,598
  • 16
  • 72
  • 115
  • Added a comment to say why I do not believe this question should not be marked as a duplicate. – jwknz Jun 23 '14 at 07:31
  • 1
    Well, the answer is clearly given in the dupe: PHP only has function scope, period. You should be able to deduce your answer from that. Regardless though, thinking of future visitors who may stumble upon this question, I think it's more useful for them to be directed to the generalized question first. There's an answer available for your specific case here if someone needs it. – deceze Jun 23 '14 at 07:37

2 Answers2

1

Now my understanding is that variables that are declared in a block of code are only able to be used in that block

Wrong, if you mean {} as a block.

According to PHP Manual

Any variable used inside a function is by default limited to the local function scope.

There is no mention of {} level scope within a function. Any variable declared inside a function is available throughout it, even if it as declared inside any sub braces. That's why its still available. Your variables like $un can be accessed even outside the loop, just that they will contain the values from last iteration.

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
0

maybe this answer might shed some light.

If I understand correctly, you answered your own question. By declaring variables on the $GLOBALS array, from within a function, you make that variable accessible throughout your scripts.

From the docs:

The $GLOBALS array is an associative array with the name of the global variable being the key and the contents of that variable being the value of the array element. Notice how $GLOBALS exists in any scope, this is because $GLOBALS is a superglobal

Community
  • 1
  • 1
Nitsan Baleli
  • 5,393
  • 3
  • 30
  • 52
  • Thanks - I will read up on that. I didn't really answer my own question, because I was asking how it was possible to access the variables without global outside of a block. – jwknz Jun 23 '14 at 07:25
  • I guess I miss-understood. I suggest up-voting the accepted answer if it solved your issue – Nitsan Baleli Jun 23 '14 at 07:34