PHP 5 - The scope of a variable includes files without classes or functions

I have read many, many threads on this and still cannot wrap myself around.

Here is my main problem:

header.php includes a file called navigation.php . Within navigation.php , $previous and $next defined. Using the echo commands, I confirmed that they matter.

Next, header.php contains 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 get no errors, but the echo instructions show that they are empty. If I do not, I get an undefined variable error.

Where exactly do I need to declare them as global so that backnext.php can read their values โ€‹โ€‹correctly?

None of these files use functions or classes.

+6
source share
2 answers

If none of these files has functions or classes, then $prev and $next are in the global scope and should be displayed by all of your included files, and you do not need to use the global .

It looks like the order of your inclusions may be a little wrong.

Update:

If I understood 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 can see both $prev and $next .

+8
source

I think that you probably have a logical error somewhere where the contents of the variables get destroyed, or as @Kev noted, the execution thread is wrong. Here are some test codes:

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 leads to the output:

 Test 2: Hi! Test 3: Hi! 

Which proves that the variable $test in the global scope, and should be available for any script after it is defined.

PS - Don't rely on SO users to provide your reference material. Go straight to the source: Scope Variable - PHP Manual The first paragraph on this page reads:

The scope of a variable is the context within which it is defined. For most PHP variables, they only have a single scope. This is the only area 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 do not get the same result both times, then there is a problem in backnext.php where the variables get wiped. If it produces the same output, then move the echoes inside backnext.php at the highest and lowest level. Logically, they donโ€™t actually move them, but you can move them closer and closer until you find where the problem is.

+1
source

Source: https://habr.com/ru/post/888657/


All Articles