【问题标题】:Some contents of an included file is not loading包含文件的某些内容未加载
【发布时间】:2013-01-20 08:38:27
【问题描述】:

我正在尝试使用 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";

【问题讨论】:

  • include_once-call 是否驻留在函数内部?
  • 粘贴 myFile.php 可能 $b 是本地变量
  • 确保所有代码(在index.php 以及myfile.php 中)都正确包装在<?php ?>-tags 中。

标签: php include require-once


【解决方案1】:

manual 中所述,Include_once 不会在成功时返回 true。

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

【讨论】:

    【解决方案2】:

    您可能没有正确包含它。

    你可以找到这个in the documentation:

    <?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';
    }
    ?>
    

    【讨论】:

    • 我要等 5 分钟 :)
    • :) 是的,我忘了我回答得太快了:P
    猜你喜欢
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-05
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多