Wuhtzu Posted February 1, 2007 Share Posted February 1, 2007 Hey I have this very simple script consisting of a function declaration and a call to the declared function: Works as stand alone but not as include: <?PHP $error['company_name'] = true; function showerror($field,$errormsg){ global $error; if($error[$field]){ echo "<span class=\"error\">".$errormsg."</span>"; } } showerror('company_name','Feltet er ikke udfyldt'); ?> The idea is that showerror() "detects" that $error['company_name'] is set and then prints the error. This works perfectly when I run the script as a standalone script (save the code to script.php and execute it) but when I include it in a larger design it stops functioning - however I can make it work again by declaring $error as global outside the function too: Works as include: <?PHP global $error; $error['company_name'] = true; function showerror($field,$errormsg){ global $error; if($error[$field]){ echo "<span class=\"error\">".$errormsg."</span>"; } } showerror('company_name','Feltet er ikke udfyldt'); ?> And that is what i need help understanding: #1: Why does the code work as a standalone script but stop functioning when i include it as a page in a lager site? #2: Why does declaring $error as global outside the function make it work again? The script included as page (both a functioning and non-functioning version): http://wuhtzu.dk/random/inc/ The script as stand alone: http://wuhtzu.dk/random/script.php *The script works if it outputs "Feltet er ikke udfyldt" Please help me uderstand this behavior Best regards Wuhtzu Quote Link to comment Share on other sites More sharing options...
corbin Posted February 1, 2007 Share Posted February 1, 2007 So from what I understand, you set errors by calling that function? I don't know what you're trying to do... Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted February 1, 2007 Author Share Posted February 1, 2007 Well the code I posted is of course of no use in reality, but it demonstrates my problem. $error['field_name'] will of course be set by another function which validates posts where 'field_name' will be the name of the field it validates and then showerror() will show the error if it is set.... So in the code i posted i just hardcoded the "$error['company_name'] = true;" because I saw no reason for posting the validation function since that isn't the problem... Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted February 2, 2007 Author Share Posted February 2, 2007 I think I didn't make my question clear enough, so I'll try again: Why do I have to use "global $error;" outside a function declaration to access the variable inside the function? I should be enough to use "global $error;" inside the function declaration.... Quote Link to comment Share on other sites More sharing options...
corbin Posted February 2, 2007 Share Posted February 2, 2007 So if you remove the global $error outside of the function it will not work when included? Care to show us the script it's being included in? I think that is where the problem most likely resides. Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted February 2, 2007 Author Share Posted February 2, 2007 YES! corbin, you are exactly right! Hang on a few minutes and I'll get the script the code is included in Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted February 2, 2007 Author Share Posted February 2, 2007 It's kind of hard to say exactly what script includes the other script, but these are the 4 relevant files: index.php: http://wuhtzu.dk/random/index_php.txt general.php: http://wuhtzu.dk/random/general_php.txt header.php http://wuhtzu.dk/random/header_php.txt footer.php: http://wuhtzu.dk/random/footer_php.txt Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted February 2, 2007 Author Share Posted February 2, 2007 but almost more important than explaining why it wont work when included, is to explain why it helps to state "global $error;" outside the function - this to me shouldn't have any meaning .... Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 2, 2007 Share Posted February 2, 2007 I had similar problems with this recently, so I'm interested to see if anyone has an answer. Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted February 2, 2007 Author Share Posted February 2, 2007 What does "global $some_var;" outside a function declaration actually do? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 2, 2007 Share Posted February 2, 2007 It brings the variable into the scope of that page. *shrug* Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted February 2, 2007 Author Share Posted February 2, 2007 Well when a variable is just declared: <?PHP $var = "foo"; ?> it is accessible throughout the script, in scripts that has been required or included - but not inside functions. This: http://no.php.net/global to me clearly states that if you wanna use a variable inside a function, which has been declared outside of the function, you have to use "global $var;" inside the function declaration. If that is the way to get access to "outside variables" what are "global $var;" used for outside of a function declaration? Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted February 2, 2007 Author Share Posted February 2, 2007 Any more ideas to why I need to declare "global $error;" both outside and inside the function.. Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 2, 2007 Share Posted February 2, 2007 I think it has to do with this line from the PHP Manual: "Any variable used inside a function is by default limited to the local function scope." By default, using $error within the function will place it into local scope. By declaring global $error; you're telling it to use the global scoped variable. However, not having declared a global version of it, it'll assume it's not real, and error out. Or so my theory is. Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted February 2, 2007 Author Share Posted February 2, 2007 Well you are right, if i just do: <?PHP $some_var = "foo"; ?> then it wont be accessible inside a function. But the way to access it inside a function, http://no.php.net/global, is to state "global $some_var;" _inside_ the function - not outside... so I wonder why it helps to declare "global $some_var;" outside the function :S Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.