8

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

db.php:

<?php
    $hostName='localhost';
    $userName='xxxxxxxxx';
    $userPass='xxxxxxxxx';
?>

and a test.php:

<?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?

11
  • 1
    Are the files in the same folder? Btw, this is not related to register_globals which is indeed removed from PHP. Apr 24, 2013 at 8:29
  • yes, both file in the same folder
    – crossRT
    Apr 24, 2013 at 8:30
  • try to print something like echo "hi" in db.php.and run test.php.this will show wether db.php is added to your file or not Apr 24, 2013 at 8:30
  • try using require instead of include - if you get an error, there is a problem loading the db.php file (could be caused by a lot of things) Apr 24, 2013 at 8:30
  • @DeepakSrinivasan yes, but it did not print anything, so i use get_included_files() to show the file that included, 'db.php' is in the list.
    – crossRT
    Apr 24, 2013 at 8:35

4 Answers 4

6

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.

1
  • 1
    I had same issue just recently, though I found the problem after some efforts. your statement is absolutely right. Feb 16, 2016 at 13:13
0

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.

8
  • i use get_required_files() and the 'db.php' is in the list. may i know how to check the log file? sorry for i'm pretty new to PHP
    – crossRT
    Apr 24, 2013 at 8:45
  • Your log file is stored in: \xampp\apache\logs\error.log. If it's not there, check the error_log setting that you can get by running phpinfo(). It's always good practice to check the log files when you run into problems :) Apr 24, 2013 at 8:48
  • yeah, i found, thanks for advice. =D But the log file did not have relevent event about the undefined variable:(
    – crossRT
    Apr 24, 2013 at 8:59
  • Nothing related to db.php? Make sure your error level is set to max Apr 24, 2013 at 9:00
  • ya, nothing even if i search in the log file. this problem making me crazy, same code but move to Hosting24 is work. @@
    – crossRT
    Apr 24, 2013 at 9:49
0

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

4
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post.
    – Atomix
    Jul 27, 2015 at 11:08
  • 2
    Hi @JoJoe for you kind information, it does answer to the question asked by the author. There is nothing to elaborate. This is surely one of the reasons that may cause the erratic issue faced here. Any novice PHP developer may also get what i meant. If you guess i am wrong then why don't you provide a answer rather then being rude to a new contributor to community. Warm Regards.
    – user1370519
    Jul 27, 2015 at 11:40
  • Hey @acoustichead, don't take this too personal, this is an automatically generated text by SO. Someone flagged your answer as low quality (perhaps because of its length), it showed up in the review queue and when I reviewed it, I marked it as "should be a comment". I did this because I thought it lacked the necessary length and explanatory depth to qualify as an answer. The system then automatically comments on the answer, admittedly not using the best choice of words, but it just isn't that flexible. If the other reviewers think your answer is fine, absolutely nothing will happen. :)
    – Atomix
    Jul 27, 2015 at 12:03
  • oh i'm sorry, this auto generated comment reads too intimidating for a new user :P
    – user1370519
    Jul 27, 2015 at 12:12
-1

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

2
  • Could you please elaborate more your answer adding a little more description about the solution you provide?
    – abarisone
    Jun 15, 2015 at 7:06
  • In the test.php , instead of include 'include.php'; , replace it with require_once('db.php');
    – Arjun
    Jun 15, 2015 at 16:19

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.