here is my code:
some html
<?php $fooArray = array("foo", "bar");
function prntFoo(){ foreach($fooArray as $value){ print($value); }
?>
some html code
<?php prntFoo() ?>
more html code
ok now the problem is I get an error that $fooArray is undefined. What could be the problem?
thanks!
Try $GLOBALS("fooArray")
or pass the array as a argument to the function
As Ellery suggests, it's better to pass the var to the function than to use globals.
<?php $fooArray = array("foo", "bar"); prntFoo($fooArray);
function prntFoo($paTheArray) { foreach($paTheArray as $value){ print($value); } ?>
or put an
global $fooArray;
in your function!
thanks for your help, is there any reason why php functions do not see global variables without using global?
Jesse
Read the manual: http://www.php.net/manual/en/language.variables.scope.php
While there are superficial similarities in the languages, variable scoping in PHP does not work like variable scoping in C.
The historical reason is this: Originally, all PHP variables were globally visible. One day Rasmus Lerdorf got bit by a namespace collision one time too many, and arbitrarily decided that functions could not "see" variables that were not specifically imported (declared).