Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP included file but Undefined variable

Tags:

php

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?

like image 886
crossRT Avatar asked Apr 24 '13 08:04

crossRT


1 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.

like image 188
mcmlxxxvi Avatar answered Nov 05 '22 17:11

mcmlxxxvi