How to better realise include?

I currently have 3 files, 1 return array, 2 is command file that manipulate all other and 3 is function file,i include array and function file in command file. How to make array visible in function file, should i use second include or there another way?

file 1

return $arr = array(...);

file 2

function FuncName(){ $arr[1] = '1111';}

file 3

include_once 'file 1';
include_once 'file 2';
$arr[2] = '2222'

Upvotes: 0

Views: 66

Answers (1)

sectus
sectus

Reputation: 15464

It's simple. You can use return inside included file.

//main.php
include_once 'functions.php';
$array = include 'array.php';
goGoArray($array);

// functions.php
function goGoArray($array){
    var_dump($array);
}

// array.php
return array(1,23,456,7890);

But, if you what global, use global.

//main.php
include_once 'functions.php';
$array = include 'array.php';
goGoArray();

// functions.php
function goGoArray(){
    global $array;
    var_dump($array);
}

// array.php
return array(1,23,456,7890);

P.S. But globals are very, very bad.

Upvotes: 1

Related Questions