开发者

How to save this code into a variable?

Is there any way to save this block of code into a PHP variable ? The reason of doing this is because I want to send it through mail()

echo 'MONDAY<BR>';
query_posts('meta_key=Date1&meta_value=MONDAY');  
while (have_posts()):
    the_post(); 
    if (in_category( '11' )) {
        echo get_post_meta($post->ID, 'HomeSlogan', true);
    } else {
        the_title();
    }
    echo'<br>';
endwhile;

This is what zne开发者_如何学运维ak suggests

<?php

ob_start();
echo 'MONDAY<br>';
query_posts('meta_key=Date1&meta_value=MONDAY');  
while (have_posts()):
    the_post(); 
    if (in_category( '11' )) {
        echo get_post_meta($post->ID, 'HomeSlogan', true);
    } else {
        the_title();
    }
    echo'<br>';
endwhile;

$mail = ob_get_contents();
echo $mail;
ob_end_clean();

?>


You can either use string concatenation and avoid echo altogether, or use output buffering. Output buffering saves your script's output into a buffer instead of sending it to the browser, so it's easier to use if you have functions that print text and that you can't really control.

// concatenation
$mail = 'MONDAY<br>';
$mail .= 'more text';
$mail .= 'yet more text';

// output buffering
ob_start();
echo 'MONDAY<br>';
echo 'more text';
echo 'yet more text';

$mail = ob_get_contents();
ob_end_clean();

For output buffering, you might want to read about ob_start, ob_get_contents and ob_end_clean.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜