简体   繁体   中英

what am i doing wrong in these php scripts?

Here is config.php

<?php
define("DB_SERVER","localhost");
define("DB_NAME","photo_galley");
define("DB_USER_NAME","root");
define("DB_PASSWORD","");
echo DB_SERVER."config file<br>";
?>

Here is classes.php

<?php
    var_dump(include_once("config.php"));
    echo DB_SERVER."class file<br/>";
?>
<?php
    class MySQLDatabase{...

and here comes third file temp.php

<?php
    include_once("includes/classes.php");
    echo DB_SERVER." tem.php <br/>";
?>

both classes.php and config.php are in includes folder, includes folder is in photo_gallery folder , temp.php is in photo_gallery folder how it looks like when i open classes.php in browser. 在此处输入图片说明

it shows thing as i thought. First show DB_SERVER value from config.php file then from classes.php file But when i include classes.php file in any other file like in temp.php as shown in third piece of code it gives something that i didn't understand here is it 在此处输入图片说明

I didn't understand output. I think it's output should be same as that of classes.php with one more line showing DB_SERVER value from temp.php.

Would you please tell me why these constants are shown as undefined when i open temp.php in browser?
if include function in line is successful as shown by int(1) then why not it show DB_SERVER value from config.php?

In temp.php your config.php file path is wrong. Thats why you got the undefined constant.

config.php file inside the includes directory. But your temp.php file outside the includes directory.

in classes.php, You can use $_SERVER['DOCUMENT_ROOT'] add full path in such way you can access the same in all files.

You include config.php from wrong location in temp.php . Remove the path. Also you should use require_once rather than include_once as config.php is crucial, so lack of it should stop the script.

When you include classes.php relative path to config.php is includes/config.php.

<?php
include_once("includes/config.php");

class MySQLDatabase{...

Your scripts are fine. You just have a problem with your server configuration.

They're working correctly in mine:

在此处输入图片说明

so we can say that when we use require include function it just copies code from file mentioned to file from which this function called. so when i call config from classes file it works fine. But when i call classes from temp it just copies code from there but for temp file config file location is different so it doesn't work. Am i correct Mr. Developers...

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