0

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.

8
  • Possible duplicate of PHP: "Notice: Undefined variable" and "Notice: Undefined index" Commented Feb 16, 2016 at 11:12
  • try to get errors using error_reporting(E_ALL ^ E_NOTICE); Commented Feb 16, 2016 at 11:20
  • @PHPExpert hi, i tried as u suggested. it suppresses the notice and the name is never echoed this way. Commented Feb 16, 2016 at 11:31
  • 1
    Use require 'config.php'; instead of include 'config.php';. This will throw a fatal error if file is not found. Commented Feb 16, 2016 at 11:32
  • @maxhb require/include same result, shows notice. i just notied my localhost include path is set to C:\xampp\php\PEAR when i did echo get_include_path(); Commented Feb 16, 2016 at 11:33

2 Answers 2

2
    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
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.