Home » Php » PHP included file but Undefined variable

PHP included file but Undefined variable

Posted by: admin July 12, 2020 Leave a comment


I’m new to PHP, and i start to use XAMPP to build the site. I have code below



and a test.php:

    include 'db.php';
    echo $hostName;

but now it show me Notice: Undefined variable: hostName

what i found is the register_globals need to set “on” in php.ini, but after i set, the XAMPP show: Directive ‘register_globals’ is no longer available in PHP when i restart XAMPP.

i move the same code to Hosting24 but it’s work, anyone can help?

How to&Answers:

I just got bit by a similar problem – definitely not OP’s problem but falls under the question’s title.

In my case I had PHP short tags disabled on the server, but the included file accidentally used short tags. When the file was included, functions were undefined and there were no inclusion errors. What I think happens in this situation is that the file is included but treated as an empty file.

If you have an “undefined” problem after inclusion/requirement, check your file tags – just in case.


The only reason that this can happen is if the file db.php is not included properly. Check your log files for why this happens.

When you include a file PHP treats this file as if you copy/pasted the code directly into the including file. So all your variables defined in the included file will have the same scope as the including file and hence are available.

In your example, it should just work. So my answer would be: check your log files for include errors. Make sure logging is set to FULL.


I had a similar issue, what happened to be the issue was the file access permissions for the files


Try using require_once('db.php') instead of include.