Jump to content

Weird behavior of script due to include


Wuhtzu

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/36713-weird-behavior-of-script-due-to-include/
Share on other sites

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...

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....

 

 

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

 

 

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?

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.