JuanBonnett
JuanBonnett

Reputation: 786

PHP - A file with a variable is included but shows UNDEFINED VARIABLE error when implemented

I have a config file which is included in every part of the website. It looks more or less like this (I removed alot of unnecesary lines for the example):

//Company info
define("COMPANY_NAME", "Test Company");
define("COMPANY_EMAIL", "[email protected]");
define("META_KEYWORDS", "");
define("META_DESCRIPTION", "");

//**********************************************************************
//Remember to change this when working Local or on the Production Server
define("DB_SERVER", "127.0.0.1"); 
define("DB_USER", "root");
define("DB_PASSWORD", "");
define("DB_SELECTED_DB", "adminpanel");

One of the lines of this config.php file contains this:

$_ITEM_TYPES = array(
    1=>"admin",
    2=>"page",
    3=>"repository"
);

I'm working in a file where this config.php is included, and I can use constants like COMPANY_NAME, COMPANY_EMAIL and so... but when I'm gonna use the $_ITEM_TYPES array, it throws an undefined variable error :/

I've used the function get_included_files() and the file is included indeed! I don't know why I cannot use this array.

PHP File menu.php

include_once("../config.php");

echo COMPANY_NAME;
//Echoes the value correctly

var_dump($_ITEM_TYPES);
//Throws error

Any ideas? Thank you all beforehand.

Upvotes: 0

Views: 115

Answers (1)

php_nub_qq
php_nub_qq

Reputation: 16015

I'm going to shoot in the dark here. I am 99% sure that the cause of your problem is that you are including this file somewhere else. By using include_once it will not include the file again because it has been included previously, however in another scope which is causing the undefined variable error.

If you want that configuration array to be available globally you can use $GLOBALS, but it's bad practice.

Upvotes: 1

Related Questions