I have a question about php include paths.
I have a site that looks like this:
site/index.php
site/sub/config.php
site/sub/functions.php
where config holds configuration variables and functions holds some reusable functions.
config:
functions:
index:
I have a couple of problems. One is that if I run functions.php, the variable is correct outside the function, but null inside. Why is that? Do I have to include config.php inside the function and outside? Do variables lose their scope moving into a function in php?
The second is that if I run index.php, all variables are null. It seems that if I change the include in functions to "./sub/config.php" then it works (other than the above problem). But this breaks functions.php running on it's own. Is there a solution to this or do I have to have one or the other?
I have a site that looks like this:
site/index.php
site/sub/config.php
site/sub/functions.php
where config holds configuration variables and functions holds some reusable functions.
config:
Code:
<?php $v = "a variable"; ?>
Code:
<?php
include "./config.php";
echo "v outside function = " . $v . "\n";
function func()
{
echo "v inside function = " . $v . "\n";
}
func();
?>
Code:
<?php
include "./sub/functions.php";
echo "v in index = " . $v . "\n";
func();
?>
The second is that if I run index.php, all variables are null. It seems that if I change the include in functions to "./sub/config.php" then it works (other than the above problem). But this breaks functions.php running on it's own. Is there a solution to this or do I have to have one or the other?