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.