Juniorflip Posted October 18, 2006 Share Posted October 18, 2006 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 https://forums.phpfreaks.com/topic/24365-load-a-variable-in-an-array/ Share on other sites More sharing options...
obsidian Posted October 18, 2006 Share Posted October 18, 2006 create your contents variable outside of your loop:[code]<?php$contents = array();foreach ($files as $file) { $contents[] = file_get_contents($file);}?>[/code] Link to comment https://forums.phpfreaks.com/topic/24365-load-a-variable-in-an-array/#findComment-110855 Share on other sites More sharing options...
Orio Posted October 18, 2006 Share Posted October 18, 2006 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 https://forums.phpfreaks.com/topic/24365-load-a-variable-in-an-array/#findComment-110856 Share on other sites More sharing options...
obsidian Posted October 18, 2006 Share Posted October 18, 2006 [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 https://forums.phpfreaks.com/topic/24365-load-a-variable-in-an-array/#findComment-110869 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.