Note: The variable is set in included file, the issue is not an undefined variable, hence the duplicate question is not relevant to my question. PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"
Edit: i just noted my localhost include path is set to C:\xampp\php\PEAR when i did echo get_include_path(); but i don't get error for included file not found rather the variable is undefined.
I have 2 files in C:/xampp/htdocs/test/
index.php
config.php
Content of index.php:
require 'config.php';
echo $name;
Content of config.php:
$name = 'xyz';
And i get Notice: Undefined variable: name
Where the $name = 'xyz'
is in config.php which is included before using the variable.
I have to yet test it on live server. Please guide.
Holly Cow.
There was no error in the file. The php start tag was not correct, it was short tag <? ?>
instead of <?php ?>
this was causing the issue.
i'm using php7 on my localhost. but of course t would work on older version of PHP and hosting.
hi i tried your code but i didn't get any error..
config.php
<?php
$name='hello';
?>
index.php
<?php
require 'config.php';
echo $name;
?>
include and require both are working