0

我正在尝试使用 include_once() php api 将文件 (myfile.php) 的内容加载到另一个 (index.php) 中。但它没有加载内容。

// contents inside index page
if(include_once('myfile.php')){
    echo D; // this is working
    echo $b; // this is not working
} else {
    echo "unable to include the file";
}   

// contents inside myfile.php
define('D', '123');
$b = "abcd";
4

2 回答 2

2

您可能没有正确包含它。

您可以在文档中找到它:

<?php
// won't work, evaluated as include(('vars.php') == 'OK'), i.e. include('')
if (include('vars.php') == 'OK') {
    echo 'OK';
}

// works
if ((include 'vars.php') == 'OK') {
    echo 'OK';
}
?>
于 2013-02-05T12:13:05.983 回答
0

如手册中所述,include_once 不会在成功时返回 true 。

您需要使用 if ((include 'myfile.php') == 'OK') {....}

于 2013-02-05T12:18:10.597 回答