Appending information to a single variable in a loop..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Appending information to a single variable in a loop..

Post 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   
}

?>
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post by AVATAr »

what kind of variable...? string? try

$string = $string . $questions

or

$string .= $questions
User avatar
swirlee
Forum Newbie
Posts: 7
Joined: Fri Dec 26, 2003 12:08 am

Post 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;
} 

?>
Post Reply