PHP: Functions have no idea variables even exist -


I have seen an annoying feature in PHP (running 5.2.11) if one page contains another page ( And both have their own variables and functions), both pages are aware of each other's variables. However, their actions are not known about any variables (except those declared within the function)

My question: Why does this happen? How can I not , or what is a better way to go about it?

I describe this type of example is an example.

Home page:

  & lt; Php $ myvar = "myvar."; ('Page2.php'); The echo says "the main script says: $ somevar and $ myvar \ n"; to work(); DoMoreStuff (); Function doStuff () {echo "main function says: $ somevar and $ myvar \ n"; } "End" echo. ;; ? & Gt;  

page2.php:

  & lt; Php $ somevar = "success!"; Echo "contains the script: $ somevar and $ myvar \ n"; Function doesMoreStuff () {echo "function" includes function: $ somevar and $ myvar \ n "; }? & Gt;  

Output:

The script involved says: Success! And mover
The main script says: Success! And mover
The main function says: And the function included in it says: and
end

Variables are just fine in both pages. Do not do their work.
WRYYYYYY

You define the global function scope Before use:

  function doStuff () {global $ somevar, $ myvar; The echo "main function says: $ somevar and $ myvar \ n"; }  

More detailed description is provided:

As an annotation & amp; Other answers correctly tell, globals may be bad tell this article why it is:


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -