Welcome Guest, Not a member yet? Register   Sign In
globals in functions in views
#1

[eluser]zauber[/eluser]
It appears that one can not use global variables in functions in views. See this example:

Code:
testview.php
---------------------------
<html><body>
<?

function printvar(){
    global $tree;
    if (isset($tree)) echo "1";
    else echo "0";
}

if (isset($tree)) echo "1";
else echo "0";
printvar();

?>
</body></html>

This outputs "10", ie: outside the function, the variable "tree" is set (passed from the controller), but inside the function the variable is NOT set, despite using "global".

Why don't globals work in functions in views? Are there any other "hidden" ways in which views are not just plain, normal php scripts?
#2

[eluser]tonanbarbarian[/eluser]
the issue is that you are assuming any variable that is declared in the view is global... it is not.
it has to do with how views are coded
it also explains why you always pass data to helpers in the view rather than letting the helper grab the data from global, because it will not be there




Theme © iAndrew 2016 - Forum software by © MyBB