Why can't I get the return value of the require_once function in PHP?

I already know that include_once will return true or false based on the inclusion of this file. I have read https://stackoverflow.com/a/2124641/ to get your value back and print it.

The problem is that I have an existing project in hand, and inside this file they return an array. I want to get the result of require_once to see what result I have, but I get 1 instead of an array that contains data:

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

What am I doing:

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

Is there any workaround for this?

+6
source share
1 answer

This meant that the file was already included at this point.

require_once will return a boolean true if the file is already included.

To check, you can change simply:

 $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 
+10
source

Source: https://habr.com/ru/post/976427/


All Articles