Php 如何改进此输出以不包含这么多html?

Php 如何改进此输出以不包含这么多html?,php,Php,我对php相当陌生,想知道如何改进这段代码。我知道它并不完美,但任何建设性的批评都是鼓励的,因为我正在努力提高自己的PHP水平。请告诉我,如果你的答案有改进的方法,你可以对答案进行一点扩展,让我知道为什么更好,这样我就可以全面了解改进的情况 public function displayArticle(){ //Check to see if we are getting the home page if($_GET['page'] == 'home'){ //D

我对php相当陌生,想知道如何改进这段代码。我知道它并不完美,但任何建设性的批评都是鼓励的,因为我正在努力提高自己的PHP水平。请告诉我,如果你的答案有改进的方法,你可以对答案进行一点扩展,让我知道为什么更好,这样我就可以全面了解改进的情况

public function displayArticle(){
    //Check to see if we are getting the home page
    if($_GET['page'] == 'home'){
        //Display the results formatted
        $content = "<article class=\"igpPost\">";
            $content .= "<div class=\"igpPost-Divider\"></div>";
            $content .= "<header>";
            $content .= "<h3><a href=\"#\">Ready Up – StarCraft 2, LoL, and Dota 2 pros head to DreamHack Summer</a></h3>";
            $content .= "<div class=\"igpPost-AuthTime\"><span>Posted By: </span><a href=\"#\">Cameron Lockhart</a> <span>at 07:44PM on June 15, 2012</span></div>";
            $content .= "<div class=\"igpPost-AuthTime\"><span>Tags: </span><a href=\"#\">TAG HERE</a> ,<a href=\"#\">TAG HERE</a> ,<a href=\"#\">TAG HERE</a> ,<a href=\"#\">TAG HERE</a> ,<a href=\"#\">TAG HERE</a> ,<a href=\"#\">TAG HERE</a></div>";
            $content .= "<div class=\"igpPost-Img\"><img src=\"images/news/DreamHack-Summer-2012-logo.jpg\"/></div>";
            $content .= "</header>";
            $content .= "<p>Did last week’s MLG Spring Championship leave you thirsting for more eSports? Then DreamHack Summer 2012 has you covered. With well-attended tournaments for StarCraft 2, League of Legends, and Dota 2, DreamHack should keep you busy throughout the weekend and into the work-week. It starts tomorrow at 11 AM Eastern, and continues through Monday, with the StarCraft 2 Grand Final scheduled for 5:15 PM Eastern.</p>";
            $content .= "<footer class=\"igpPost-Footer\">";
            $content .= "<div class=\"igpPost-ReadMore\">";
            $content .= "<h1><a href=\"#\">Read More..</a></h1>";
            $content .= " </div>";
            $content .= "</footer>";
            $content .= "</article>";
    }
        //If it is not the home page and it is a single article
        if($_GET['article']){
            //Display the article formatted
        }
}
公共函数displayArticle(){
//检查一下我们是否能看到主页
如果($_GET['page']=='home'){
//显示格式化的结果
$content=“”;
$content.=“发布人:2012年6月15日晚上7:44”;
$content.=“标记:,,”;
$content.=”;
$content.=”;
$content.="上周的MLG春季锦标赛让你渴望更多的电子竞技吗?那么,2012年夏季梦幻黑客大赛就已经开始了。由于星际争霸2、传奇联盟和Dota 2的比赛人满为患,梦幻黑客将让你在整个周末和工作周都很忙。它将于明天上午11点开始,一直持续到周一,星际争霸2总决赛定于东部时间下午5:15举行。

“; $content.=”; $content.=”; $content.=”; $content.=”; $content.=”; $content.=”; } //如果不是主页,而是一篇文章 如果($\u获取['article']){ //显示已格式化的文章 } }
另外,这显然不是一个完整的脚本,但看看它,它看起来对PHP来说很重要。我读了一些教程,我认为他们给了我一个错误的方向,正确地使用了好的PHP

更新:我浏览并试图修改一些代码,以便提供更具描述性的概述:

$sql = "SELECT * FROM articles LIMIT $number";
        $stmt = $pdo->query($sql);
        $stmt->setFetchMode(PDO::FETCH_ASSOC);

        while($row = $stmt->fetch()){
            //Display the results formatted
            $content = "<article class=\"igpPost\">";
            $content .= "<div class=\"igpPost-Divider\"></div>";
            $content .= "<header>";
            $content .= "<h3><a href=\"index.php?article=" . $row['id'] ."\">" . $row['title'] . "</h3>";
            $content .= "<div class=\"igpPost-AuthTime\"><span>Posted By: </span><a href=\"#\">" . $row['author'] . "</a> <span>at " . formatDateTime($row['datetime']) . "</span></div>";
            $content .= "<div class=\"igpPost-AuthTime\"><span>Tags: </span>";
            $content .= "<div class=\"igpPost-Img\"><img src=\"" . $row['imglocation'] ."\"/></div>";
            $content .= "</header>";
            $content .= "<p>" . $row['content'] . "</p>";
            $content .= "<footer class=\"igpPost-Footer\">";
            $content .= "<div class=\"igpPost-ReadMore\">";
            $content .= "<h1><a href=\"index.php?article=" . $row['id'] ."\">Read More..</a></h1>";
            $content .= " </div>";
            $content .= "</footer>";
            $content .= "</article>";

            echo $content;
        }
$sql=“从文章中选择*限制$number”;
$stmt=$pdo->query($sql);
$stmt->setFetchMode(PDO::FETCH_ASSOC);
而($row=$stmt->fetch()){
//显示格式化的结果
$content=“at”.formatDateTime($row['datetime']);
$content.=“标签:”;
$content.=”;
$content.=”;
$content.=“”$row['content']。“

”; $content.=”; $content.=”; $content.=”; $content.=”; $content.=”; $content.=”; echo$内容; }

这就是我要做的,我基本上是想把html和PHP分开,但在需要的地方插入动态内容。这都在一个类中。

你可以在这里使用各种各样的东西,包括heredoc语法(与你所拥有的直接等价物):


如果可以,请退出PHP,除非您需要对
$content
变量执行其他操作。我不确定您何时/何地/如何调用函数,但我假设在进行一些检查之后,什么都没有

<?php 
  //Do any checks/validation etc here before you send something out to the server

?>
<html><head><!--HEADER STUFF HERE--></head>
<body>
<!-- anything you want before your content here -->

<?php
if($_GET['page'] == 'home'){
?>
<div class="igpPost-Divider"></div>
<header>
 ....
</footer>
</article>
<?php
}
  //If it is not the home page and it is a single article
    if($_GET['article']){ ?>
       <!-- HTML for article here --> 
    <?php
    }
?>
</body>
</html>

....

我不知道如何减少HTML,因为我不知道您的具体需求,但是,您可以使用HEREDOC大大简化PHP代码

herdoc()是一种在as中输出字符串和变量的方法,使用双引号。不同之处在于不需要在as中转义任何引号

您的代码可以使用如下所示的HEREDOC:

$sql = "SELECT * FROM articles LIMIT $number";
    $stmt = $pdo->query($sql);
    $stmt->setFetchMode(PDO::FETCH_ASSOC);

    while($row = $stmt->fetch()){
        //Display the results formatted
        $dateTime = formatDateTime($row['datetime']) // functions cannot be used in HEREDOCS, so I replace it with the variable $dateTime declared here
        $content = <<<HERE
<article class="igpPost">
<div class="igpPost-Divider"></div>
<header>
    <h3><a href="index.php?article="$row['id']">$row['title']</h3>
    <div class="igpPost-AuthTime"><span>Posted By: </span><a href="#">$row['author']</a> <span>at $dateTime</span></div>
    <div class="igpPost-AuthTime"><span>Tags: </span>
    <div class="igpPost-Img"><img src="$row['imglocation']"/></div>
    </header>
    <p>$row['content']</p>
    <footer class="igpPost-Footer">
    <div class="igpPost-ReadMore">
    <h1><a href="index.php?article="$row['id']">Read More..</a></h1></div>
    </footer>
</article>
HERE;
echo $content;
}
$sql=“从文章中选择*限制$number”;
$stmt=$pdo->query($sql);
$stmt->setFetchMode(PDO::FETCH_ASSOC);
而($row=$stmt->fetch()){
//显示格式化的结果
$dateTime=formatDateTime($row['dateTime'])//函数不能在HEREDOCS中使用,因此我将其替换为此处声明的变量$dateTime

$content=内容是否始终是静态的?虽然在类函数中使用
$\u GET
并不坏,但我建议将其作为参数从外部传递,例如
displayArticle($type)
,并在内部执行
切换($type)
看起来像意大利面代码,超时会让所有东西都很难看。我还没有真正对PHP做过任何认真的工作,在更大范围内,这种风格可能变得难以管理。但这种方式是我喜欢PHP的一个方面,你可以随意进出HTML,甚至可以使用if和forI等内部控制结构如果他们在工作时编写了这样的代码,有几件事要说。这对初学者来说是完美的,但不是一种值得鼓励的方法。调试非常困难,缩进是一场噩梦,一般来说不可能阅读。不记分数(因为你花时间编写了一个很好的示例,对于初学者来说可能是正确的)但这真的不是教人前进的正确方法。很抱歉这么坦率。这句话的作者是:at$dateTime“这可能是一个问题。我认为#in看起来像是对php的一个评论。因此,您需要在那里解决问题。是的,这只是html草稿的一个位置。我肯定会继续使用herdoc。我非常感谢您的帮助和解释。
<?php 
  //Do any checks/validation etc here before you send something out to the server

?>
<html><head><!--HEADER STUFF HERE--></head>
<body>
<!-- anything you want before your content here -->

<?php
if($_GET['page'] == 'home'){
?>
<div class="igpPost-Divider"></div>
<header>
 ....
</footer>
</article>
<?php
}
  //If it is not the home page and it is a single article
    if($_GET['article']){ ?>
       <!-- HTML for article here --> 
    <?php
    }
?>
</body>
</html>
$sql = "SELECT * FROM articles LIMIT $number";
    $stmt = $pdo->query($sql);
    $stmt->setFetchMode(PDO::FETCH_ASSOC);

    while($row = $stmt->fetch()){
        //Display the results formatted
        $dateTime = formatDateTime($row['datetime']) // functions cannot be used in HEREDOCS, so I replace it with the variable $dateTime declared here
        $content = <<<HERE
<article class="igpPost">
<div class="igpPost-Divider"></div>
<header>
    <h3><a href="index.php?article="$row['id']">$row['title']</h3>
    <div class="igpPost-AuthTime"><span>Posted By: </span><a href="#">$row['author']</a> <span>at $dateTime</span></div>
    <div class="igpPost-AuthTime"><span>Tags: </span>
    <div class="igpPost-Img"><img src="$row['imglocation']"/></div>
    </header>
    <p>$row['content']</p>
    <footer class="igpPost-Footer">
    <div class="igpPost-ReadMore">
    <h1><a href="index.php?article="$row['id']">Read More..</a></h1></div>
    </footer>
</article>
HERE;
echo $content;
}