Jump to content


Photo

Load a variable in an array


  • Please log in to reply
3 replies to this topic

#1 Juniorflip

Juniorflip
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 18 October 2006 - 07:29 PM

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

$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();


#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 18 October 2006 - 07:54 PM

create your contents variable outside of your loop:
<?php
$contents = array();
foreach ($files as $file) {
  $contents[] = file_get_contents($file);
}
?>

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 18 October 2006 - 07:55 PM

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





Was:Should be:
function create_xml() function create_xml($outFileName2)
create_xml();create_xml($outFileName2);


Orio.
Think you're smarty?

(Gone until 20 to November)

#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 18 October 2006 - 08:26 PM

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





Was:Should be:
function create_xml() function create_xml($outFileName2)
create_xml();create_xml($outFileName2);


Orio.


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

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 ;)
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users