PHP脚本中的PHP回声


PHP Echo Within PHP Script

我有一个包含echo命令的php脚本,但它没有正确显示在我的网页上。这是代码:

<?php
$content_sql = "SELECT * FROM mag_books WHERE id = '$id'";
$content_res = mysqli_query($con, $content_sql);
while($content = mysqli_fetch_assoc($content_res)){
    $content_intro = nl2br($content["intro"]);
    $display_content = "
        <div class='"pageSection text'">
            $content_article
        </div>
        <div class='"pageSection text'">
            $folder = 'files/books/'.$post_year.'/'.$post_id.'/';
            $filetype = '*.{jpg}*';
            $files = glob($folder.$filetype, GLOB_BRACE);
            foreach ($files as $file)
            {
                echo '
                        <div class='"galleryCell'">
                            <img class='"galleryPhoto'" src='"files/books/'.$file.''" />
                        </div>
                ';
            }
        </div>
    ";
};
?>

在我的网站上显示:

= 'files/books/'.2016.'/'.1463391024.'/'; = '*.{jpg}*'; = glob(., GLOB_BRACE); foreach ( as ) { echo ' '; }

如何转义代码以显示应该输出的内容?

您没有将php代码分配给variable.您可以像这样使用

 $display_content ='';
 $display_content .= "<div class='"pageSection text'">".$content_article."</div><div class='"pageSection text'">";
                $folder = 'files/books/'.$post_year.'/'.$post_id.'/';
                $filetype = '*.{jpg}*';
                $files = glob($folder.$filetype, GLOB_BRACE);
                foreach ($files as $file)
                {

                    $display_content .="<div class='"galleryCell'"><img class='"galleryPhoto'" src='"files/books/".$file."'" /></div>";
                }
               $display_content .= "</div>";
    echo $display_content;

这样试试:

$display_content = '';
while($content = mysqli_fetch_assoc($content_res)){
    $content_intro = nl2br($content["intro"]);
    $display_content .= '
        <div class="pageSection text">
            ' . $content_article . '
        </div>
        <div class="pageSection text">';
            $folder = 'files/books/'.$post_year.'/'.$post_id.'/';
            $filetype = '*.{jpg}*';
            $files = glob($folder . $filetype, GLOB_BRACE);
            foreach ($files as $file) {
                $display_content .= '
            <div class="galleryCell">
               <img class="galleryPhoto" src="files/books/'.$file.'" />
            </div>';
            }
        $display_content .= '
        </div>';
}
echo $display_content;