【问题标题】:PHP. Why return in included file dosen't assign with variable?php。为什么在包含的文件中返回不分配变量?
【发布时间】:2015-04-08 06:47:49
【问题描述】:

例如我有 2 个文件:

//db.php
return array(
   'server'=>'localhost',
   'db'=>'mydb',
   'name'=>'root',
   'password'=>'root',
);

第二个文件:

//index.php
$config = (include 'db.php');

之后$config = 1(如果包含文件)或$config = 0(如果不包含文件),但$configdb.php 文件中必须是array()

什么问题?

windows 7x64、apache 2.2、php 5.4

【问题讨论】:

  • php.net/manual/en/function.include.php,示例#5包含和返回语句
  • 我知道,我可以使用什么函数,但这不是我的项目,他有那个代码,那个代码在服务器上工作,但在我的本地主机上不起作用......
  • 没有明显的理由为什么它不应该工作。确保您包含正确的文件,并且在路径中的其他位置没有另一个同名的文件可能是实际包含的文件
  • 执行@Mark 建议的最简单方法:echo 'This is the correct file!' in db.php
  • 你是什么意思“他显示数组()”......你发布的代码实际上并没有显示任何东西......你有一个<?php在顶部你的db.php 文件?

标签: php include return


【解决方案1】:

您可以将 INI 文件用于您的数据库凭据。不要忘记拒绝访问 .htaccess 文件中的 config.ini。

config.ini

[database]
server = localhost
db = mydb
name = root
password = root

index.php

<?php
    $config = parse_ini_file('config.ini');

    // Access to credentials
    echo $config['server'];
?>

【讨论】:

  • 我之前怎么说,这不是我的项目,我和其他开发人员一起做他,我们使用 VCS。我不想更改加载配置的核心代码。
  • 虽然,是的,没错,但它没有回答问题。
【解决方案2】:

来自deceze的真实答案 短开标签!!! &lt;? The array is not supposed to be output at all; if it is, something's wrong with your file. In this case, PHP isn't configured to handle short open tags

【讨论】:

    猜你喜欢
    • 2010-12-26
    • 2021-12-27
    • 1970-01-01
    • 2022-01-22
    • 2016-03-24
    • 1970-01-01
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多