PHP string concatenation

I have set $message variable. Now I wana concatenate foreach loop’s results to existing $message


<img style="vertical-align: text-bottom" src="<?=$user->img($folder_img, $folder_alt, false, '', 'src');?>" title="<?=$user->lang[$folder_alt];?>" alt="<?=$user->lang[$folder_alt];?>" />
            <a href="<?=$phpbb_root_path . 'viewtopic.php?f=' . $t['forum_id'] . '&amp;t=' . $t['topic_id'] ;?>"><?=html_entity_decode($t['topic_title']);?></a><br />

My Complete code is below


<?
    $message .= $msg\
";


    // Get active topics.
    $sql="SELECT *
    FROM " . TOPICS_TABLE . "
    WHERE topic_approved = '1' AND " . $db->sql_in_set('forum_id', $forums) . "
    ORDER BY topic_last_post_time DESC";
    $result = $db->sql_query_limit($sql,TOPICS_LIMIT);
    while ($r = $db->sql_fetchrow($result))
    {
        $topics[] = $r;
    }
   $db->sql_freeresult($result);

   // How can I add "foreach" loop's output to $message
   $message .=

    foreach($topics as $t)
    {
        // Get folder img, topic status/type related information
        $topic_tracking_info = get_complete_topic_tracking($t['forum_id'], $t['topic_id']);
        $unread_topic = (isset($topic_tracking_info[$t['topic_id']]) && $t['topic_last_post_time'] > $topic_tracking_info[$t['topic_id']]) ? true : false;
        $folder_img = $folder_alt = $topic_type = '';
        topic_status($t, $t['topic_replies'], $unread_topic, $folder_img, $folder_alt, $topic_type);

        // output the link
        ?>
            <img style="vertical-align: text-bottom" src="<?=$user->img($folder_img, $folder_alt, false, '', 'src');?>" title="<?=$user->lang[$folder_alt];?>" alt="<?=$user->lang[$folder_alt];?>" />
            <a href="<?=$phpbb_root_path . 'viewtopic.php?f=' . $t['forum_id'] . '&amp;t=' . $t['topic_id'] ;?>"><?=html_entity_decode($t['topic_title']);?></a><br />
    <?
    }
    ?>

// How can I add “foreach” loop’s output to $message
$message .=

In principle:


$array = array('this', 'that', 'the', 'other');

$msg = "" ; // message is an empty string to start with, say.

$msg .= "Today my talk is about: ";  // no space between .= !!

foreach( $array as $element ){
$msg .= $element . " "; // add a space between each word
}

echo $msg . ". Thank you.";

// Today my talk is about: this that the other . Thank you.

There are other ways of adding array elements to a string, but to answer your question this ought to illustrate.

ps this line


    $message .= $msg\
";

should be


    $message .= $msg . "\
";

And if a line end is what you want you could do this:


    $message .= $msg . PHP_EOL; // php end of line

// An OS specific line end

thank you so mush Cups for your valuable suggestions. Sorry for being late, I was out of town. Now I have changed my script according to your guidelines. Everything is working perfectly except two things. I wana send automatic email notifications to Admin on my forums. Whenever a new thread is started Admin get email. Now I am trying to add recent x number topic to email message.

test.php


foreach($topics as $t)
    {
        // Get folder img, topic status/type related information
        $topic_tracking_info = get_complete_topic_tracking($t['forum_id'], $t['topic_id']);
        $unread_topic = (isset($topic_tracking_info[$t['topic_id']]) && $t['topic_last_post_time'] > $topic_tracking_info[$t['topic_id']]) ? true : false;
        $folder_img = $folder_alt = $topic_type = '';
        topic_status($t, $t['topic_replies'], $unread_topic, $folder_img, $folder_alt, $topic_type);


        // output the link
			
		$newposts .= '<img style="vertical-align: text-bottom" src="' . $user->img($folder_img, $folder_alt, false, '', 'src') . '" title="' . $user->lang[$folder_alt] . '" alt="' . $user->lang[$folder_alt] '" />


       		<a href="' .$phpbb_root_path . 'viewtopic.php?f=' . $t['forum_id'] . '&amp;t=' . $t['topic_id'] . '">' . html_entity_decode($t['topic_title']) . '</a><br />';
    }

        $_SESSION['new_posts'] = $newposts;

posttomail.php



include('test.php');

$message .= "<div>" . $_SESSION['new_posts'] . "</div>\
";


  1. Initially variable $newposts was declared. I changed it to $_SESSION[‘new_posts’] because $newposts was not being passed to posttomail.php file. Any Idea ?

  2. If I remove

$newposts .= '<img style="vertical-align: text-bottom" src="' . $user->img($folder_img, $folder_alt, false, '', 'src') . '" title="' . $user->lang[$folder_alt] . '" alt="' . $user->lang[$folder_alt] '" />

these lines from test.php file, it works perfectly without the image. To display the image these lines are required.