简体   繁体   中英

Use of undefined constant [variable_name] - assumed [variable_.name] error in Php

Brief :

I am working on a very simple php project in which i had made a config.php file which holds the all application/configuration variable required to maintain the website for eg: [database/server/uername/pass etc.]

now when i call a variable from config file it throws an error.

Generation of Problem.

Hierarchy of files :

 - /..
 - lib     
 :  :
 :  - config.php
 :  - include.php
 :
 - index.php

code of config.php

<?php
define('database','dbTest');
?>

code of include.php

<?php
include('config.php');
echo database; // comment this line when u make index.php
?>

[if u run include.php using xampp u will probably get the out put as dbTest reason is simple as we have included config.php we are able to access the database variable]

Now the problem is when we include include.php file in another file.

code of index.php

<?php
include('lib/include.php');
echo database;
?>

now when i run index.php i get the following error.

error

Notice: Use of undefined constant database - assumed 'database' in C:\\xampp\\htdocs..\\test\\lib\\sam.php on line 3 database

can you please guide me how to solve this problem.

Change the include in include.php to:

include(__DIR__ . '/config.php');

That's because relative paths are relative to index.php not include.php . But __DIR__ will give you an absolute path to the folder where include.php is stored.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM