Page 1 of 1
passing an array with include
Posted: Thu Mar 29, 2012 1:02 pm
by LarsUlrich
Hi.
Maybe a silly question, but:
can I pass an array in the Include statement?
I have the script A.php, where I define an array, and then the statement
And I want the data in the array to be accesible in B.php
How can be done?
Thanks in advance.
Re: passing an array with include
Posted: Thu Mar 29, 2012 1:14 pm
by requinix
Everything available in A.php will be available in B.php. Same functions, same variables, same values.
Re: passing an array with include
Posted: Thu Mar 29, 2012 1:28 pm
by LarsUlrich
Thanks requinix.
Then I have something wrong in the rest of the script, because it doesn't seem to be reading the array.
Anyway, knowing that it should be available helps me a lot for finding the error.
Thanks again.
Re: passing an array with include
Posted: Thu Mar 29, 2012 2:02 pm
by requinix
If you're not sure where the problem is, feel free to post A.php.
My guess is something with variable scope. For example,
Code: Select all
$array = array();
function setarray() {
$array[] = 1;
$array[] = 2;
$array[] = 3;
}
setarray();
include "B.php";
wouldn't work because the $array outside of setarray() is not the same $array that's inside it. The one on the outside isn't being modified.
It's not that B.php isn't getting the value, it's that the value isn't what you think/expect/want it to be.
Re: passing an array with include
Posted: Thu Mar 29, 2012 5:04 pm
by LarsUlrich
Thanks again requinix.
I got it. The array was ok, the problem was in the handler (B.php).
In my scheme,
A.php sends variables with post method (user input).
B.php handles this variables, add some more (user input), and calls C.php.
C.php connects to a database, changes things in the db, get some of this values in an array and send it back again to B.php.
The problem in B.php is that I mixed handling several same variables coming through the post method (i.e., coming from A.php) with those coming in the array (from C.php).
Thanks for pointing at what to look.