Jump to content

HTML inside PHP or PHP inside HTML, what to do?


Ge64

Recommended Posts

I've used PHP for a while and every time I do this differently, but I don't think I'm happy with any of the methods.

 

In the end, a PHP file gets evaluated and becomes HTML, but most of the time you need to put in HTML elements like <table> and <div> in order to form your basic layout, but you add in PHP code in between and all over the place.

 

So, do you start out with a PHP file and add in HTML where you need it? If so, how do you add in the HTML, do you echo it or do you make holes in the PHP?

 

OR do you start with a proper HTML page and put in PHP parts wherever you need PHP, meaning you'll have <?php echo ... ?> all over the place as if there were HTML tags.

 

If you are making a table out of what you get from a MySQL database, do you just have a script somewhere that processes the data, then a HTML table with <?php echo $avalue; ?> inside it, or do you make a php script with echo "<tr><td>" . $avalue . "</tr></td>"; in it? What is the best way to do it, what is the best way to keep it looking neat and organised and what is the best way performance-wise?

Link to comment
Share on other sites

It's almost entirely user preference, though it does depend on the situation.

 

For situations with lots of HTML and little PHP, i close the PHP tag and only echo variables where i need them. For something with lots of variables, i'd echo everything. As i say though, this is personal preference.

 

There are of course other altenatives, like the heredoc syntax.

Link to comment
Share on other sites

I prefer either the HEREDOC syntax, usually if I know the script are only for my editing, if its a commercial client project and the

client needs access to templates I use Smarty Templates.

 

Heredoc syntax is great, and underused by many, you just need to be careful with the closing Heredoc marker, sometime

editors add space after the marker and it causes no end of havoc ;)

 

$message = 'Goodbye';
print <<<_SIGNOFF

   <h1>{$message}</h1>

_SIGNOFF:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.