Hahah. That's okay! All is good!
Printable View
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.
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.
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.PHP Code:<?php
$username = "username";
?>
<?php
echo "<table>";
echo "<tr>";
echo "<td>" . $username . "</td>";
echo "</tr>";
echo "</table>";
?>
This is the correct way to pull information,
info.php:
index.php:PHP Code:<?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>';
}
?>
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.PHP Code:<html>
<?php include('info.php'); ?>
<body>
<table>
<tr><th>Column</th><th>Value</th></tr>
<?php getUserInfo(); ?>
</table>
</body>
</html>
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.
Neither.