/    Sign up×
Community /Pin to ProfileBookmark

‘Connecting’ 2 php files via the content of one variable –is it possible?

Hi all,

file1.php yields (output):
x[0] = 50
x[1] = 100
x[2] = 150

‘x’ has to be an input variable for file2.php –I can’t merge file1 and file2; I have to use different files.

Any idea on how to link file1 and file2 via ‘x’ content?

Thanks,
Ruben

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@bokehMar 11.2008 — Ruben, your question doesn't make any sense, except possibly to you.
Copy linkTweet thisAlerts:
@TheRaveMar 11.2008 — When you say "can't merge", can you still include? As in include("file2.php"); If you can include then there's no problem.
Copy linkTweet thisAlerts:
@rvazquezauthorMar 11.2008 — Hope this contributes to clarify my point…

My file1.php initializes an array variable x. For example:

x[0] = 50; x[1] = 100; x[2] = 150;

But I want to access variable x from another file (file2.php) to perform further processing.

I can’t join content of both file1.php and file2.php into a single file, but instead, I have to use different files. So, I wonder how to access variable x, which is initialized in file1.php, from other file: file2.php.

Comments welcome,

Ruben

PS: In the previous post, I implicitly proposed a (potential) solution: using the output of file1.php as the input of file2.php –in case this can be done.
Copy linkTweet thisAlerts:
@TJ111Mar 11.2008 — Make a test-script like this:
[code=php]
require("file1.php");
//initialized variable
print_r($x);
[/code]

Using require makes sure that the file is included and the variable will be initialized. PHP parses included files in the order they are recieved, and treats them as inline scripts.

for example:

file1.php
[code=php]
function included_func($x) {
return var_dump($x);
}
[/code]

file2.php
[code=php]
$var = array ("text"=>6, "more_text"=>5);

require("file1.php");

print included_func($var);
[/code]
Copy linkTweet thisAlerts:
@TheRaveMar 11.2008 — *got there b4 me*
Copy linkTweet thisAlerts:
@bokehMar 11.2008 — x[0] = 50; x[1] = 100; x[2] = 150;[/QUOTE]That's not a PHP array. Is it javascript?
Copy linkTweet thisAlerts:
@TJ111Mar 11.2008 — Its a PHP array, its just assigning them in associative key=>value pairs.

Here's a little example I ran in my PHP command-line debugger.
<i>
</i>$x = array("test"=&gt;5, "example"=&gt;6)
: array =
test: long = 5
example: long = 6
$x[0]
: undefined = NULL
$x['test']
: long = 5


Edit: Wow, I'm an idiot. Sorry Bokeh. His array would be a PHP array if he added $ before each x, he could of just left it out for example purposes.
Copy linkTweet thisAlerts:
@bokehMar 11.2008 — Its a PHP array, its just assigning them in associative key=>value pairs.[/QUOTE]The array the OP has posted is not a PHP array.
×

Success!

Help @rvazquez spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...