Cory Nickerson
Cory Nickerson

Reputation: 903

Possible issue with PHP config?

I got a new laptop and installed a new version of XAMPP. I moved over a system I'm working on and it isn't functioning properly on this computer.

First issue I recognized was it doesn't include files from the location relative to the file that is including it. I have a config.php file in the includes/ directory as well as my include.php file. In the include.php file I have to type require 'includes/config.php'; even though the config.php is in the same folder as the include.php. On my other computer I just needed to do require 'config.php';.

Also since the included files didn't load with my main file which I am viewing, the variables and constants aren't defined, so it seems to be auto-defining them?

Notice: Use of undefined constant DB_HOST - assumed 'DB_HOST' in C:\xampp\htdocs\Xion\includes\include.php on line 6

Is this an issue with the config on a newer version of PHP?

Upvotes: 0

Views: 101

Answers (2)

Jonathan Kuhn
Jonathan Kuhn

Reputation: 15311

The notice error is because you are accessing an array like $someArray[DB_HOST]. Array keys should have quotes around them like $someArray['DB_HOST']. No quotes is a constant and keys are strings, not constant.

The include path issue is pretty standard too. If you have the following files:

index.php
includes/config.php
includes/include.php

If you load index.php and include the file includes/include.php you will have to type the full path includes/include.php. If inside of include.php you want to include includes/config.php. php treats the path as relative to where the original script is loaded which was index.php. Think about it like:

  • In script, you load and run index.php.
  • In index.php you include includes/include.php. This doesn't move you to includes/include.php but rather gets includes/include.php and brings the code into index.php
  • All the code from includes/include.php runs as if from within index.php.
  • You include includes/config.php. You are still technically in index.php and so you have to reference the file as if calling from within index.php.

To fix the include path, you can use the set_include_path function and add the full path to the includes directory (or relative to includes) and then you can drop the includes/ from the path. PHP will just check if the file is in includes/ if it can't find it anywhere else. You can also just change the include_path directive in php.ini or an .htaccess file.

Upvotes: 0

raina77ow
raina77ow

Reputation: 106443

Usually PHP core setting include_path contains the current path as well: it's denoted by . (a dot), added to a list of included pathes:

include_path=".;c:\php\includes"

Quoting the doc:

Using a . in the include path allows for relative includes as it means the current directory. However, it is more efficient to explicitly use include './file' than having PHP always check the current directory for every include.

And yes, any barename (a non-quoted string) will be processed as a constant name by PHP. If this constant is not defined (like in your case), PHP will convert it to a string (issuing a notice, though).

Upvotes: 1

Related Questions