PHP 5 - Variable scope across include files with no classes or functions
I've read through many, many threads on this and still can't wrap my head around it.
Here's my basic issue:
header.php
includes a file called navigation.php
. Within navigation.php
, $previous
and $next
are defined. Using echo statements I have verified they have values.
Next, header.php
includes a file called backnext.php
. I need backnext.php
to know the values of $previous
and $next
. If I declare them as global
at the top of backnext.php
, I don't get errors, but echo
statements show they are empty. If I don't, I开发者_JAVA技巧 get an undefined variable
error.
Where exactly do I need to declare them as global
to have backnext.php
be able to read their values correctly?
None of these files are using functions or classes.
If none of these files have functions or classes then $prev
and $next
are in the global scope and should be seen by all your include files and you shouldn't need to use the global
keyword.
It sounds like the order of your includes may be a bit wrong.
Update:
If I understand correctly you have something like this:
header.php:
<?php
echo "In header.php\n";
require_once("navigation.php");
require_once("backnext.php");
echo "Also seen in header.php:\n";
echo "prev=$prev\n";
echo "next=$next\n";
?>
navigation.php:
<?php
echo "In navigation.php\n";
$prev = "Hello World#1";
$next = "Hello World#2";
echo "Exiting navigation.php\n";
?>
backnext.php:
<?php
echo "In backnext.php\n";
echo "prev=$prev\n";
echo "next=$next\n";
echo "Exiting backnext.php\n";
?>
If I run this I get:
In header.php In navigation.php Exiting navigation.php In backnext.php prev=Hello World#1 next=Hello World#2 Exiting backnext.php Also seen in header.ph prev=Hello World#1 next=Hello World#2
backnext.php
is able to see both $prev
and $next
.
I think you probably have a logic error somewhere where the contents of the variables are getting wiped, or like @Kev pointed out the flow of execution is wrong. Here is some test code:
File: test.php
<?php
$test = "Hi!";
require_once 'test2.php';
require_once 'test3.php';
?>
File: test2.php
<?php
echo("Test 2: " . $test . "<br/>");
?>
File: test3.php
<?php
echo("Test 3: " . $test . "<br/>");
?>
This produces the output:
Test 2: Hi!
Test 3: Hi!
Which proves that the variable $test
is globally scoped, and should be available to any script after it is defined.
PS- Don't rely on users of SO to provide your reference material. Go straight to the source: Variable Scope - PHP Manual First paragraph on that page reads:
The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope. This single scope spans included and required files as well.
Edit
Try this in header.php
and see what happens:
<?php
include 'navigation.php';
echo($previous . " ; " . $next . "<br/>");
include 'backnext.php';
echo($previous . " ; " . $next . "<br/>");
?>
If you don't get the same output both times, then there's a problem in backnext.php
where the variables are getting wiped. If it does produce the same output, then move the echoes inside backnext.php
at the very top and very bottom. Logically its not actually moving them, but you can keep moving them closer and closer together until you find where the issue is.
精彩评论