简体   繁体   中英

PHP config file transfer variables?

I'm a php noob, so bear with me...

I have two files here. Both are in the SAME directory

I have a config file:

config.php

<?php
date_default_timezone_set('America/Chicago');

define('APP_NAME',"/control");
define('HTTP_SERVER', 'http://localhost/');
define('SITE_NAME', 'http://localhost/');
define('DOCUMENT_ROOT', $_SERVER['DOCUMENT_ROOT'].APP_NAME);
?>

Then I have my index file:

index.php:

<!DOCTYPE html>
<html>
<head>
<?php
require ('config.php');
echo "<script type='text/javascript' src='".DOCUMENT_ROOT."/scripts/jquery.js"'></script>;
?>
.....THE REST OF MY HTML.....

But I get an error that "DOCUMENT_ROOT" is undefined. Why is it not pulling the value from the config.php file?

You need to dereference your variable using $:

$DOCUMENT_ROOT

That is assuming it's been properly defined in the server environment.

回显末尾有一个小的语法错误,否则对我来说很好。

echo "<script type='text/javascript' src='".DOCUMENT_ROOT."/scripts/jquery.js\"'></script>";

Change this line: (missing ending quote " after </script> and $ sign in front of DOCUMENT_ROOT )

I tested the following on my server and the script did load, since I do have jquery.js on mine.

echo "<script type='text/javascript' src='".DOCUMENT_ROOT."/scripts/jquery.js"'></script>;

to:

echo "<script type='text/javascript' src='$DOCUMENT_ROOT/scripts/jquery.js'></script>";

or:

echo "<script type='text/javascript' src='".DOCUMENT_ROOT."/scripts/jquery.js'></script>";

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