I’m trying to have something like a configuration file, and store in it variables.
When I’m trying to include it, I can’t access the variables. it gives me this error:
Notice: Undefined variable: myVar
include.php
<?php
$myVar = "Text";
?>
index.php
<?php
include('include.php');
?>
<html>
.....
<body>
<?php
echo $myVar;
?>
</body>
P.S. I heard it’s not a good habit to have this file with variables and include it in every page. if it is, please show me the better way to do this.
Thanks.
If you’re thinking about using include.php to use as a database sort of, then I’ld suggest reading about MySQL and start using that instead.
If you’re only looking to carry on a single variable, your code should be all right.
I don’t know if it’s just your message, or if you do the same in your code, but echo $myVar must be inside a php tag
Also, make sure you’ve entered the correct path to your file in the include() function, and make sure it’s called before trying to output the variable.