Jump to content

Load a variable in an array


Juniorflip

Recommended Posts

I need to load a variable into an array here is my code so far

[code]
$ourFileName2 = $ourFileName;

Function create_xml() {
$files = array('header.xml',$ourFileName2,'footer.xml');

foreach($files as $file)
{
$contents[] = file_get_contents($file);
}

$fp = fopen('new_file.xml','w+');
fwrite($fp,join("\n",$contents));
fclose($fp);

}
create_xml();[/code]
Link to comment
Share on other sites

You need to pass the variable to the function, so:

[table]
[tr][td][b]Was:[/b][/td][td][b]Should be:[/b][/td][/tr]
[tr][td]function create_xml() [/td][td]function create_xml($outFileName2)[/td][/tr]
[tr][td]create_xml();[/td][td]create_xml($outFileName2);[/td][/tr]
[/table]

Orio.
Link to comment
Share on other sites

[quote author=Orio link=topic=111937.msg453963#msg453963 date=1161201325]
You need to pass the variable to the function, so:

[table]
[tr][td][b]Was:[/b][/td][td][b]Should be:[/b][/td][/tr]
[tr][td]function create_xml() [/td][td]function create_xml($outFileName2)[/td][/tr]
[tr][td]create_xml();[/td][td]create_xml($outFileName2);[/td][/tr]
[/table]

Orio.
[/quote]

why? he's statically declaring the file within the function itself with this line:
[code]
<?php
$fp = fopen('new_file.xml','w+');
?>
[/code]

i can't understand what you're recommending here (or at least how it would help). i believe his issue is simply a problem with variable scope. he's trying to access a variable from outside the loop that was created within. if he declares the variable before he gets into the loop and simply modifies it in there, he should be ok as long as all the variables actually contain what he thinks they do ;)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.