Page 1 of 1

Appending information to a single variable in a loop..

Posted: Fri Dec 26, 2003 2:04 pm
by Jim
I've got an array.
I've got a foreach loop.
I've got one variable.

Each time the foreach loop runs, I want information from a specified array appended on to the end of a specified variable.

This is what I'm trying to accomplish:

Code: Select all

<?

foreach($q_array as $questions)
{
  // This code will cause the $q_var variable to be overwritten each time the loop passes, which is what I don't want.  How do I append each piece of information to the end of the variable?
     $q_var = $questions   
}

?>

Posted: Fri Dec 26, 2003 2:50 pm
by AVATAr
what kind of variable...? string? try

$string = $string . $questions

or

$string .= $questions

Posted: Fri Dec 26, 2003 3:06 pm
by swirlee
Don't forget to initialize outside the loop (pesky Notices):

Code: Select all

<?
$q_var ='';

foreach($q_array as $questions) 
{ 
     $q_var .= $questions;
} 

?>