Outputting HTML with echo considered bad practice

2019-01-18 13:47发布

问题:

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.

回答1:

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.



回答2:

There's nothing wrong with echo for html, when used in moderation. Just don't use it for long multi-line blocks. You'll invariably end up with some ugly construct requiring escaping and whatnot, which makes things even uglier to read.

If the html you're outputting is "static" (no variables to insert), then consider breaking OUT of php mode (?>) and simply dumping the html as is. If you do need to insert variables, then consider using a HEREDOC, which act like a double-quoted string, but without the quotes.



回答3:

Why not write it like this?

<?php if($_SESSION['loggedIn'] == 1): ?>
    <div id='main'>MAIN MENU stuff goes here</div>
<?php else: ?>
    <div id='main'>Please login...</div>
<?php endif; ?>

Using the alternative control structures seperates your markup from your code a bit more.



回答4:

<?php if (condition) { ?>
     <div> 
        some stuff
     </div> 
<?php } ?>

The beauty of PHP is that you can do this.



回答5:

If your project gets to a reasonable size, there's essentially no way around a clean separation of presentational elements and program logic, just for the sake of maintainability and scalability. So what you're doing in your current code doesn't really matter; in the long run, you should look into a clean design approach from the outset.

There are many existing solutions, usually involving some sort of layout templates which are loaded by the code.



回答6:

There is no true 'best practice', although some will argue that they prefer one or the other. Ideally if you separate HTML from PHP, you're isolating the backend of your application from the frontend, therefore making it easier to read, modify and maintain.

As for your code, I might modify it so it is more concise (depending on what you mean by 'MAIN MENU stuff ...' I'd reconsider this edit):

<div id="main">
    <?= ($_SESSION['loggedIn'] == 1) ? 'MAIN MENU stuff goes here' : 'Please login...'; ?>
</div>


回答7:

Some consider it bad practice, that's true of anything. I like to not use echo. If you do it this way it is more clear to editors such as Dreamweaver what you want and get all the autocomplete benefits.

<?php
if ($loggedin)
{
?>
Thank you for being logged in. <hr>
<?php
}
else
{
?> 
Please <a href='login.php'>login</a>
<?php
}
?>


标签: php html echo