Echoing HTML output is considered bad practice in PHP?

In PHP, I use the if to determine if the user is logged in or not, and depending on the result, displays the main menu (if registered) or the message โ€œyou need to log inโ€ if not. I do it like this:

 <!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 that displays either the main menu or the message "please log in" is produced by echo . Is this a bad practice, maybe there is a better way?

By the way, I cut most of the HTML from echo in my snippet above. The main menu is made up of a list, but I was not worried that, since it is not relevant to the issue, I think.

+8
html php echo
source share
7 answers

I think this is bad practice. Not sure if anyone else is thinking. Firstly, it looks awful in text editors with syntax highlighting, then you need to worry about escaped lines, etc.

Here 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 jump with php tags and use the html code directly. There are pros and cons to doing it this way. I like it better than echo. Other options are to take a new look in these areas based on the results of if statements. Many possibilities, but above is just one way to make it a little cleaner and (I think) better.

+24
source share

There is nothing wrong with echo for html when used in moderation. Just do not use it for long multi-line blocks. You will always end up with some ugly construction that requires shielding and something else that makes things even uglier to read.

If the html you are outputting is "static" (no variables to insert), then consider exiting the php mode ( ?> ) And just reset the html as it is. If you need to insert variables, consider using HEREDOCs , which act like a string with two quotes, but no quotes.

+6
source share

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 alternative control structures separates your markup from your code a bit more.

+5
source share
 <?php if (condition) { ?> <div> some stuff </div> <?php } ?> 

The beauty of PHP is that you can do it.

+3
source share

If your project is approaching a reasonable size, there is essentially no way to get around the clean separation of presentation elements and program logic just for convenience and scalability. So what you are doing in your current code is not a big deal; Ultimately, you should think about clean design from the start.

There are many existing solutions, usually including some layout templates that are loaded with code.

+2
source share

There is no true โ€œbest practice,โ€ although some argue that they prefer one or the other. Ideally, if you separate HTML from PHP, you isolate the backend of the application from the external interface, which makes it easier to read, modify, and maintain.

As for your code, I can change it to be more concise (depending on what you mean by "MAIN MENU stuff ...", I will revise this edit):

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

Some consider it a bad practice no matter what. I like not to use echo. If you do this, editors such as Dreamweaver are all the more clear what you want and get all the benefits of autocomplete.

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

All Articles