• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

The "Correct" Way

Which method do you prefer?

  • Method 1 (Source code less readable, output indented)

    Votes: 3 27.3%
  • Method 2 (Source code is more readable, output is not indented.

    Votes: 8 72.7%

  • Total voters
    11
Skilled Illusionist
Joined
Jun 21, 2010
Messages
324
Reaction score
54
Hello,

I've just recently discovered a blog that discusses how to implement PHP into your HTML programming and the result if you did it this way and that way and so on.

However, their was a topic discussed that involved techniques on how one may accomplish a task in HTML using PHP. The topic involved an array, and how one may print it into their HTML environment. The two results were vastly different.

Array:
PHP:
<?php
$names = array("John", "Jack", "Anna", "Ray", "Bob");
?>

Method 1: (which is now the way I prefer)
PHP:
<ul>
<?php foreach($names as $name) { ?>
	<li><?php echo $name; ?></li>
<?php } ?>
</ul>

Method 2: (which is the method I dropped)
PHP:
<ul>
<?php
foreach($names as $name)
{
	echo "<li>" . $name . "</li>";
}
?>
</ul>

Method 1 implies that your HTML output is indented, however the source or from a developer's point of view is a little difficult to read.

Method 2 implies that your source code appears neater and more readable, however the output is messy and not indented.

My question to you, which do you prefer or would call the correct way of doing this task.
 
Skilled Illusionist
Joined
Jun 21, 2010
Messages
324
Reaction score
54
That's what I thought, but the output appears neater and doesn't look like it's been rushed. I'm usually pretty fussy with my projects though :/
 
Joined
Jul 26, 2006
Messages
3,634
Reaction score
1,007
Well first of all, you should consider what you're trying to achieve. Thing I've been learning over the past few weeks is that you should really separate the front- and backend of your applications.

e.g. using a few php scripts to put/pull things to/from the database, returning data in JSON and using ajax calls in JS in html files (with the viewmodel system that knockout.js implements) at the client-side to show results.

That way, you don't even need to think of stuff like this, 'cause it'll look good anyway!
 
may web.very maple.pls.
Loyal Member
Joined
Aug 12, 2009
Messages
1,810
Reaction score
606
My way:
PHP:
<?php
$meh = '';
 foreach($names as $name) { 
   $meh .= "<li>{$name}</li>";
}
?>

<ul><?=$meh;?></ul>
 
Skilled Illusionist
Joined
Jun 21, 2010
Messages
324
Reaction score
54
I was always had the vision that trying to output HTML via PHP is bad practice and/or not recommended and therefore is good practice to only print out what is explicitly needed which would be the values.

I guess what I'm saying is that, this way you let HTML do its job and PHP do its job. This way I also believe that you are not stressing PHP out, with unnecessary process(s). However I know it wouldn't be much of a process, but for instance in a larger application this could be problematic.

I don't know if I'm right or wrong, correct me if I am, however this is what I believe also.
 
Google my name...
Joined
Nov 9, 2011
Messages
483
Reaction score
151
I was always had the vision that trying to output HTML via PHP is bad practice and/or not recommended and therefore is good practice to only print out what is explicitly needed which would be the values.

I guess what I'm saying is that, this way you let HTML do its job and PHP do its job. This way I also believe that you are not stressing PHP out, with unnecessary process(s). However I know it wouldn't be much of a process, but for instance in a larger application this could be problematic.

I don't know if I'm right or wrong, correct me if I am, however this is what I believe also.

Have fun making a system that lists users from a database!

PHP outputting HTML is not bad practice and no one really says it's not recommended, in-fact a lot of basic systems like listing registered users or even something like a forum in which the threads are listed require PHP to output HTML, there are a lot of other methods you could use, i.e. Javascript calling the data from an API and building it but this is client side meaning the output will not load on-page load but once the page has fully loaded.

PHP is a server-side script utilising the server's capabilities, not the clients. It is better to have all the content on your webpage when it is loaded by a client than to load it onto the screen after the client has loaded the page...

All the best,
Richard Komakech.
 
Skilled Illusionist
Joined
Jun 21, 2010
Messages
324
Reaction score
54
Have fun making a system that lists users from a database!

PHP outputting HTML is not bad practice and no one really says it's not recommended, in-fact a lot of basic systems like listing registered users or even something like a forum in which the threads are listed require PHP to output HTML, there are a lot of other methods you could use, i.e. Javascript calling the data from an API and building it but this is client side meaning the output will not load on-page load but once the page has fully loaded.

PHP is a server-side script utilising the server's capabilities, not the clients. It is better to have all the content on your webpage when it is loaded by a client than to load it onto the screen after the client has loaded the page...

All the best,
Richard Komakech.

When I say "output to HTML via PHP" I am referring to this:

PHP:
<?php
$username = "username";
?>

<?php
echo "<table>";
echo "<tr>";
echo "<td>" . $username . "</td>";
echo "</tr>";
echo "</table>";
?>

I believe the above example does not show consistency throughout the application, hence allowing PHP to undertake unnecessary processes.

I believe the same task could be done, and allowing for more consistency throughout your application.

PHP:
<?php
$username = "username";
?>

<table>
	<tr>
		<td><?php echo $username; ?></td>
	</tr>
</table>

That being said, you let HTML do it's job and PHP do it's job. I don't think you'll agree, but that is what I'm saying. Back to my main point, you are only printing out what needs to be printed, the values.
 
Skilled Illusionist
Joined
Jun 21, 2010
Messages
324
Reaction score
54
Have fun making a system that lists users from a database!

PHP outputting HTML is not bad practice and no one really says it's not recommended, in-fact a lot of basic systems like listing registered users or even something like a forum in which the threads are listed require PHP to output HTML, there are a lot of other methods you could use, i.e. Javascript calling the data from an API and building it but this is client side meaning the output will not load on-page load but once the page has fully loaded.

PHP is a server-side script utilising the server's capabilities, not the clients. It is better to have all the content on your webpage when it is loaded by a client than to load it onto the screen after the client has loaded the page...

All the best,
Richard Komakech.

In regards to your statement and I quote, "Have fun making a system that lists users from a database!". I don't see your humour, for some reason. Anyhow..

I've just quickly simulated a database in which you print out the data. I stand by what I say when I say I believe this is letting HTML do its job, and PHP doing its job. The script is consistent, HTML is separate from PHP. PHP is not echoing HTML, however, the PHP is echoing the value, which goes inside the HTML element.

Anyway, it's here, just quickly paste it into your web server, and have quick examine of what it does.

View Here
 
Newbie Spellweaver
Joined
May 26, 2011
Messages
59
Reaction score
23
You could also use the short-syntax:
PHP:
<ul>
    <?php foreach($elements as $element) : ?>
        <li><?= $element ?></li>
    <?php foreachend; ?>
</ul>
That makes everything much cleaner.

You could also use this to echo some HTML code:
PHP:
echo <<<HTML
    <form method="post" action"">
        <label for="username">Username</label>
        <input type="text" name="username" id="username">

        <label for="password">Password</label>
        <input type="password" name="password" id="password">

        <input type="submit">
    </form>
HTML;
 
Praise the Sun!
Loyal Member
Joined
Dec 4, 2007
Messages
2,502
Reaction score
986
You could also use the short-syntax:
PHP:
<ul>
    <?php foreach($elements as $element) : ?>
        <li><?= $element ?></li>
    <?php foreachend; ?>
</ul>
That makes everything much cleaner.

You could also use this to echo some HTML code:
PHP:
echo <<<HTML
    <form method="post" action"">
        <label for="username">Username</label>
        <input type="text" name="username" id="username">

        <label for="password">Password</label>
        <input type="password" name="password" id="password">

        <input type="submit">
    </form>
HTML;

Bgkl8 - The "Correct" Way - RaGEZONE Forums
 
Google my name...
Joined
Nov 9, 2011
Messages
483
Reaction score
151
@ACEDEMiC
I must've misunderstood what you meant then, I thought you literally meant PHP should never interact with HTML, wether that be echoing out a name or doing a foreach loop.
Now I have seen your replies and understand what you mean. Thank you for reiterating that. I agree with your method, I do not think HTML tags should ever be echoed by PHP although there is actually nothing wrong in doing so.

All the best,
Richard Komakech.
 
Skilled Illusionist
Joined
Jun 21, 2010
Messages
324
Reaction score
54
@ACEDEMiC
I must've misunderstood what you meant then, I thought you literally meant PHP should never interact with HTML, wether that be echoing out a name or doing a foreach loop.
Now I have seen your replies and understand what you mean. Thank you for reiterating that. I agree with your method, I do not think HTML tags should ever be echoed by PHP although there is actually nothing wrong in doing so.

All the best,
Richard Komakech.

Hahah. That's okay! All is good!
 
Google my name...
Joined
Nov 9, 2011
Messages
483
Reaction score
151
it's the other way around

How is it the other way round? You'd rather a client loads your webpage then the content is loaded after with a "Loading" box and a few seconds delay as your front-end language requests the backend (API or not) for data and displays it on the page. How is that better than loading a webpage and having all the relevant content pre-loaded onto it?

All the best,
Richard Komakech.
 
Joined
Apr 28, 2005
Messages
6,953
Reaction score
2,420
I'd like to clarify something which doesn't seem to have been said..

Outputting HTML in PHP is completely fine. In fact, it is nearly unavoidable and doing it really doesn't affect performance that much. The point here is to only output content that is required to be dynamic via PHP, and not outputting all content in a section.

This was posted a few posts back.

PHP:
<?php 
$username = "username"; 
?> 

<?php 
echo "<table>"; 
echo "<tr>"; 
echo "<td>" . $username . "</td>"; 
echo "</tr>"; 
echo "</table>"; 
?>

This is what a lot of people will do because it is in most cases faster to code this way. Using this method of displaying information is what people refer to when they say "don't use PHP to display HTML". You're wrapping elements that will not be dynamic.

This is the correct way to pull information,

info.php:
PHP:
<?php
function getUserInfo()
{
    $infoarr = // call a DB result array here
    echo '<tr><td>User: </td><td>'.$infoarr['username'].'</td></tr>';
    echo '<tr><td>Email: </td><td>'.$infoarr['email'].'</td></tr>';
}
?>

index.php:
PHP:
<html>
<?php include('info.php'); ?>
<body>
    <table>
        <tr><th>Column</th><th>Value</th></tr>
        <?php getUserInfo(); ?>
    </table>
</body>
</html>

This way you're generating the required HTML outside of PHP, and only using PHP to generate HTML when content for that specific section needs to be dynamic. This is the best way I can explain it. Obviously there are APIs to further decrease HTML generation via PHP, but this is the best way you'll get around expanding towards using those.

If you're using OOPHP then you'll never want to return HTML from your functions; strictly values only. You'll then pass these values to a single function which is called to display content to the browser to be wrapped in minimal HTML. Your back-end code should never be passing around any sort of HTML to be embedded in to your site, with obvious exceptions for content management systems for returning news posts with markup.

Hope this cleared up a few things.
 
Last edited:
Skilled Illusionist
Joined
Jun 21, 2010
Messages
324
Reaction score
54
I'd like to clarify something which doesn't seem to have been said..

Outputting HTML in PHP is completely fine. In fact, it is nearly unavoidable and doing it really doesn't affect performance that much. The point here is to only output content that is required to be dynamic via PHP, and not outputting all content in a section.

This was posted a few posts back.

PHP:
<?php 
$username = "username"; 
?> 

<?php 
echo "<table>"; 
echo "<tr>"; 
echo "<td>" . $username . "</td>"; 
echo "</tr>"; 
echo "</table>"; 
?>

This is what a lot of people will do because it is in most cases faster to code this way. Using this method of displaying information is what people refer to when they say "don't use PHP to display HTML". You're wrapping elements that will not be dynamic.

This is the correct way to pull information,

info.php:
PHP:
<?php
function getUserInfo()
{
    $infoarr = // call a DB result array here
    echo '<tr><td>User: </td><td>'.$infoarr['username'].'</td></tr>';
    echo '<tr><td>Email: </td><td>'.$infoarr['email'].'</td></tr>';
}
?>

index.php:
PHP:
<html>
<?php include('info.php'); ?>
<body>
    <table>
        <tr><th>Column</th><th>Value</th></tr>
        <?php getUserInfo(); ?>
    </table>
</body>
</html>

This way you're generating the required HTML outside of PHP, and only using PHP to generate HTML when content for that specific section needs to be dynamic. This is the best way I can explain it. Obviously there are APIs to further decrease HTML generation via PHP, but this is the best way you'll get around expanding towards using those.

If you're using OOPHP then you'll never want to return HTML from your functions; strictly values only. You'll then pass these values to a single function which is called to display content to the browser to be wrapped in minimal HTML. Your back-end code should never be passing around any sort of HTML to be embedded in to your site, with obvious exceptions for content management systems for returning news posts with markup.

Hope this cleared up a few things.

Although it's hard to understand where you're coming from, I think I get the gist of what you're saying. However, I still believe that functions should "only" ever handle PHP data and PHP values and never to output any kind of HTML content or tags. I mean PHP functions should only be throwing around to other functions data and values, that is integers, booleans and so on. However their is absolutely nothing wrong with echoing or outputting (a better word) HTML data at all, and I mean that. I also believe that their would be exceptions, however I'm speaking in general.

But, I believe you should follow standards when creating a product that is designed to educate other users because doing this will encourage other users to properly use or efficiently (is a better word) use and manipulating how a program is designed to operate.

In conclusion, at the end of the day, outputting HTML is not wrong, nor is it frowned upon nor is it something you should never do. However, I have witnessed people digest these topics and when they get to a point where they make their own web application that makes use of these functions. The functions are usually overly complicated and tend to repeat, and make the process longer when they are not realizing it could be reduced.
 
Back
Top