Because you aren't taking into account variable scope. PHP like most languages keeps it's variables separate from each other, so you can use the same variable name in differnet scopes and they won't interfear with each other.
<?php
$test = "This is a test<br>";
print $test;
print test_func();
print $test;
function test_func() {
$test = "So is this<br>";
return $test;
}
That code will output:
This is a test
So is this
This is a test
Showing you that your $test variable inside the global scope (everything not in a function or class) is kept separate from your variable $test inside the function. If you want to access the information outside of your fuction I suggest that you return it to the calling scope (global in this case).
<?php
$a = build_array();
echo $a["color"]; // should print red
function build_array() {
$a = array ("color" => "red");
return $a;
}
?>