I have a page with a variable and a function:
test.php
<?php
$myVar = "myVar";
function myFunc() {
echo "myFunc()";
}
?>
The first page that includes it, can see the variable value.
<?php
echo "page1";
require_once("include/test.php");
print_r($myVar);
function GoTo2() {
$urlSite = "page2.php";
include($urlSite);
exit();
}
GoTo2();
?>
but the second page, cannot see the variable.
<?php
echo "page2n";
require_once("include/test.php");
print_r($myVar);
?>
it generates PHP Warning: Undefined variable $myVar in .../page2.php on line 4
I cannot use require()
in the second page, because it generates an error – PHP Fatal error: Cannot redeclare myFunc() (previously declared in .../include/test.php) in .../include/test.php ...
What is the proper php way that both pages can see the variable and also can use the function?
EDIT:
- this is a simplified of my real pages. I just discovered that page2 cannot see the variable when the site was upgraded to php8, and I recieved all "Undefined variable" warnings.
- page1 is a "dispatcher" page
- suppose I inherited this code from another developper, what is the best solution to solve the problem?
2
Answers
First problem is that the second include will not happen because it’s already included.
Second problem is that you cannot use variables from outside the function. You need to either send the variable as a parameter in the function or you can make the variable to be global
First solution:
function GoTo2($myVar) {...
Second solution:
funtion GoTo2() { global $myVar; ...
Since your real question is not "why is this happening", but rather "what is the proper way of doing it", then theoretically the whole scheme would need a rethink, as pointed out by @ADyson.
Basically, you have that
page1.php
includestest.php
andpage2.php
, which then also includestest.php
again. But the logic ofincludes
is that you should include files only once.It is not wrong to
include
variable definitions, like you would do, for instance, in the case of a configuration file. But then again: youinclude
configurations only once; if it happens twice, it’s most probably a bug, or a misconception. The same applies with libraries of functions.There’s really no "only right way to do it". It would depend on what’s available to you, and what you intend. That being said, an ideal design could be to gather all configurations in one file; the functions gathered in one or more files, according to their specificity; and have a core file that
include
whichever files you need for the particular operation. For example, a structure like this:If you have many files to include, you could make a proxy configuration file:
configuration.php
Then, your
page1.php
would be:The concept of "sub-pages" (like a
page2.php
called withinpage1.php
) is not necessarily wrong either, although it could reveal itself tricky: you’ll have to keep in mind that this piece of code is meant to always be called by another page, and presuppose all the configurations that have been done prior to call.So the thing, here, is that a given page (in your case,
page2.php
) ideally is not supposed to be called both directly and by another page (in your case,page1.php
).Either
page1.php
andpage2.php
are both always called directly, and thus load separately their own configurations (page1.php
includestest.php
, andpage2.php
includestest.php
), orpage2.php
is always loaded bypage1.php
, and then relies on it for its own configurations (page1.php
includestest.php
, thenpage2.php
: herepage2.php
will have access to the configurations intest.php
already loaded bypage1.php
).