用echo输出HTML在PHP中被认为是不好的做法? [英] Outputting HTML with echo considered bad practice in PHP?

查看:199
本文介绍了用echo输出HTML在PHP中被认为是不好的做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中,如果语句使用语句来标识用户是否已登录,并根据结果显示主菜单(如果已记录)或者你需要登录的消息,如果没有的话。我是这样做的:

 <!DOCTYPE html PUBLIC -  // W3C // DTD XHTML 1.0 Strict // EN
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8/>
< link rel =stylesheethref =style.csstype =text / css/>
< title>主页< / title>
< / head>
< body>
< div id =header>
< a href =index.php>< img src =wtcdblogo.pngalt =WTC DB logo/>< / a>
< / div>
<?php
if($ _ SESSION ['loggedIn'] == 1){
echo< div id ='main'> MAIN MENU东西到这里< / div> ;;
} else {
echo< div id ='main'>请登录...< / div>;
}
?>
< / body>
< / html>

正如您所看到的,显示主菜单或请登录消息的代码是由 echo 产生。这是不好的做法,也许有更好的办法?

顺便说一下,我从上面代码片段的 echo s中删除了大部分HTML。主菜单是由一个列表组成的,但我并没有打扰包括它,因为它与问题无关,我猜。 解决方案

我认为这是不好的做法。不确定其他人的想法。首先,在文本编辑器中使用语法高亮显示它看起来很糟糕,那么你不得不担心转义字符串等。



这就是我的做法:

 < div> 
<? if($ _SESSION ['loggedIn'] === 1):?>
< div id =main>主菜单东西放在这里< / div>
<?其他:?>
< div id =main>请登入...< / div>
<? endif?>
< / div>

您可以跳出PHP标签并直接使用HTML。这样做有利有弊。我喜欢这种方式比回应东西更好。其他选择是根据if语句的结果对这些领域提出新的观点。很多可能性,但以上只是一个方法,使一点清洁和(我认为)更好。


In PHP, I'm using an if statement to identify whether a user is logged in or not, and depending on the result, displaying the main menu (if logged in) or a "you need to login" message if not. I am doing this like so:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <link rel="stylesheet" href="style.css" type="text/css" />
    <title>Home</title>
</head>
<body>
    <div id="header">
       <a href="index.php"><img src="wtcdblogo.png" alt="WTC DB logo" /></a>
    </div>
    <?php 
       if($_SESSION['loggedIn'] == 1) {
          echo "<div id='main'>MAIN MENU stuff goes here</div>";
       } else {
          echo "<div id='main'>Please login...</div>";
       } 
    ?>
</body>
</html>

As you can see, the code to display either the main menu or the "please login" message is produced by an echo. Is this bad practice, perhaps there's a better way?

By the way, I've cut out most of the HTML from the echos in my snippet above. The main menu is made up of a list, but I didn't bother including that as it's irrelevant to the question, I guess.

解决方案

I consider it to be bad practice. Not sure about what anyone else thinks. For one thing, it looks terrible in text editors with syntax highlighting, then you have to worry about escaped strings, etc.

This is how I do it:

   <div>
      <? if ($_SESSION['loggedIn'] === 1): ?>
         <div id="main">Main Menu stuff goes here</div>
      <? else: ?>
         <div id="main">Please log in...</div>
      <? endif ?>
   </div>

You can hop out of the PHP tags and use straight up HTML. There are pros and cons to doing it this way. I like it way better than echoing stuff out. Other options would be to render new views into those areas based on the results of the if statements. Lots of possibilities, but the above is just one way to make that a little cleaner and (I think) better.

这篇关于用echo输出HTML在PHP中被认为是不好的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆