简体   繁体   English

为什么我无法在PHP中获取require_once函数的返回值?

[英]Why I cannot get the return value of require_once function in PHP?

I already know that include_once would return true or false based on including that file. 我已经知道include_once会根据包含该文件返回truefalse I've read a question on Stackoverflow about using require_once to return your value and print it out. 在Stackoverflow上读过一个关于使用require_once返回值并将其打印出来的问题。

The problem is that I have an existing project in hand, and inside of that file they return an array. 问题是我手头有一个现有项目,并且在该文件内部返回一个数组。 I want to get the output of require_once to see what result I've got, but I get 1 instead of array that contains data: 我想获取require_once的输出以查看我得到的结果,但是我得到的是1而不是包含数据的array

return array('data'=>$result_data,'error'=>null);

What I do is: 我所做的是:

$ret = require_once $this->app->config('eshopBaseDir')."fax/archive.php";
print_r($ret);

Is there any workaround for this? 这有什么解决方法吗?

This indicated that the file has already been included at that point. 这表明该文件已包含在该点。

require_once will return boolean true if the file has already been included. 如果文件已被包含,require_once将返回布尔值true。

To check you can change to simply require: 要检查您可以更改为只需要:

$ret = require $this->app->config('eshopBaseDir')."fax/archive.php";
print_r($ret);

As a simple proof: 作为一个简单的证据:

//test.php

return array('this'=>'works the first time');

//index.php

$ret = require_once 'test.php';
var_dump($ret);//array
$ret2 = require_once 'test.php';
var_dump($ret2);//bool true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM