如何将php文件的输出读入变量?

时间:2018-01-28 00:45:44

标签: php

我有一个类似于以下

的php文件
templateUrl: '/{PATH_TO_THIS_FILE}/editUser.html',

我想将php输出的数组设置为变量。所以我的变量是实际的数组。我试着做以下

<?php

return [
    'first' => 'some value',
    'second' => 'blah blah',
];

但是,将文件内容作为字符串返回。

如何$config = file_get_contents(__DIR__ . '/config.php'); 成为$config文件中返回的数组?

3 个答案:

答案 0 :(得分:2)

函数file_get_contents()按照documentation的字符串返回文件。请改用include_once(),如下所示:

$config = include_once __DIR__ . '/config.php';

答案 1 :(得分:1)

您必须更改文件,删除strftime()并创建作业,如下所示:

return

然后,如果您需要在不同的脚本中使用该数组,请使用includeinclude require

PS:考虑做一些关于PHP的基础教程。你现在问的是基本问题,重新发明轮子需要花费很多时间。

答案 2 :(得分:-1)

您可以使用Eval。请注意使用它的风险:When is eval evil in php?