How much PHP, how much HTML

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

43 Replies - 3627 Views - Last Post: 01 February 2011 - 12:26 PM Rate Topic: -----

#1 diego_pmc  Icon User is offline

  • D.I.C Addict

Reputation: 81
  • View blog
  • Posts: 565
  • Joined: 13-May 09

How much PHP, how much HTML

Posted 20 January 2011 - 07:00 AM

I have to read from a database and display a table on my webpage. What is better, to have most of the code in HTML and only have PHP tags around the PHP instructions, like this...

<table>
    <tr>
        <td><?php /* read data */?></td>
        <td><?php /* read some other data */?></td>
    </tr>
</table>


... or should everything be PHP?...

echo "<table>";
echo "    <tr>";
/* etc */
echo "    </tr>";
echo "<table>";


I'd inclined to choose the first option. (Unless there is a better option, like separating HTML from PHP even further.)

And a slightly unrelated question: if a page has no PHP code, is there any reason not to give it a HTML extension?

This post has been edited by diego_pmc: 20 January 2011 - 07:05 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How much PHP, how much HTML

#2 Creecher  Icon User is offline

  • I don't care
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,049
  • Joined: 06-March 10

Re: How much PHP, how much HTML

Posted 20 January 2011 - 07:13 AM

You can do a multi line echo instead of using a bunch of echos.


echo '<table>
    <tr>
        <td> /* read data */</td>
        <td>/* read some other data */</td>
    </tr>
</table>';
?>



Or you can put it into a home made function.


Like so:

<?php
function ShowWhatever()
{
	echo'<table>
    <tr>
        <td> /* read data */</td>
        <td>/* read some other data */</td>
    </tr>
</table>';
}
?>



Also, to answer your question:

I prefer to use all of the same file extensions on my websites. So I use nothing but .php files :P

This post has been edited by NeverPool: 20 January 2011 - 07:14 AM

Was This Post Helpful? 0
  • +
  • -

#3 shezzy  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 194
  • Joined: 28-January 07

Re: How much PHP, how much HTML

Posted 20 January 2011 - 07:35 AM

Best thing to do i think is use php alternative syntax. It helps keep html/php code cleaner imo.
Read more here


Here is an example:

<?php   if($i==1): ?>
<p><?php echo $text; ?> </p>
<p><?php echo 'this text will only show if $i is equal to one';?></p>

<?php endif; ?>


what it does is create a block for the if condition and only processes the block of html if $i is equal to 1. This method is very useful for MVC which is where I learned its value.
Was This Post Helpful? 1
  • +
  • -

#4 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: How much PHP, how much HTML

Posted 20 January 2011 - 07:43 AM

Given the choice between the two I would go with the first option of doing <td><?php;?</td> however I would advice against that as well.

You don't want to get into the habit of mixing your html/php/javascript into one file, it's bad practice and is messy to look at modify. Instead try to maintain code separation using a templating engine like Smarty or by using a different method like one shown in this tutorial

Working with projects separated properly into layers makes creating maintaining software much, much easier. You can spend more time working with the code and making it do what you want it to than trying to decipher a convoluted mess.

This post has been edited by Jstall: 20 January 2011 - 07:44 AM

Was This Post Helpful? 4
  • +
  • -

#5 diego_pmc  Icon User is offline

  • D.I.C Addict

Reputation: 81
  • View blog
  • Posts: 565
  • Joined: 13-May 09

Re: How much PHP, how much HTML

Posted 20 January 2011 - 09:53 AM

@shezzy I was actually thinking of doing it like that but I never tried it — I thought when I close the PHP tag, the script would no longer have any effect on the HTML code that follows and I immediately assumed it wouldn't work. :D

@Jstall Thanks, CTphpnwb's tutorial is exactly what I was hoping for! His code looks wonderful!

This post has been edited by diego_pmc: 20 January 2011 - 09:54 AM

Was This Post Helpful? 0
  • +
  • -

#6 Xtron  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 28
  • Joined: 20-May 09

Re: How much PHP, how much HTML

Posted 25 January 2011 - 04:07 PM

Also, use single quotes not double. Single quotes are better for performance so it's better practice to use them. Double quotes are only needed if you have a variable in your string and it needs to be formatted before being displayed.
Was This Post Helpful? 1
  • +
  • -

#7 diego_pmc  Icon User is offline

  • D.I.C Addict

Reputation: 81
  • View blog
  • Posts: 565
  • Joined: 13-May 09

Re: How much PHP, how much HTML

Posted 27 January 2011 - 11:41 AM

I did notice it's better to use single quotes in PHP. If not for anything else, then at least for being able to do echo 'class="current"' instead of echo "class=\"current\"". I wasn't aware of the performance increase though and neither about how double-quote strings indicate the compiler it should check for variables inside of strings.
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3476
  • View blog
  • Posts: 10,007
  • Joined: 08-June 10

Re: How much PHP, how much HTML

Posted 27 January 2011 - 01:48 PM

View PostXtron, on 26 January 2011 - 12:07 AM, said:

Single quotes are better for performance so it's better practice to use them.

Benchmarks prove that wrong.
Was This Post Helpful? 2
  • +
  • -

#9 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,000
  • Joined: 08-August 08

Re: How much PHP, how much HTML

Posted 27 January 2011 - 06:05 PM

If you've separated html and php quotes won't matter to php because you'll only (well, mostly) use them in the html. ;)
Was This Post Helpful? 0
  • +
  • -

#10 diego_pmc  Icon User is offline

  • D.I.C Addict

Reputation: 81
  • View blog
  • Posts: 565
  • Joined: 13-May 09

Re: How much PHP, how much HTML

Posted 01 February 2011 - 02:33 AM

View PostCTphpnwb, on 27 January 2011 - 06:05 PM, said:

If you've separated html and php quotes won't matter to php because you'll only (well, mostly) use them in the html. ;)


The only place where I do that is in navigation menus...
<li<?php echo ($this->current == 'home') ? ' id="current"' : '' ?>>...</li>


... and when alternating table row colors...
<?php foreach ($this->tableRows as &$row) { ?> 
<tr class=<?php echo ($i++ % 2) ? '"odd"' : '"even"' ?>>
	<td>...</td>
</tr>
<?php } ?>


Do you think I shouldn't be doing that?

This post has been edited by diego_pmc: 01 February 2011 - 02:39 AM

Was This Post Helpful? 0
  • +
  • -

#11 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3476
  • View blog
  • Posts: 10,007
  • Joined: 08-June 10

Re: How much PHP, how much HTML

Posted 01 February 2011 - 02:44 AM

View Postdiego_pmc, on 01 February 2011 - 10:33 AM, said:

The only place where I do that is in navigation menus...

... and when alternating table row colors...

you should do neither of those. these are really, really bad examples of mixing HTML and PHP.

$li = "<li%s>  </li>";
printf($li, ($this->current == 'home') ? ' id="current"' : '');

$tr = <<<ROW
<tr class="%s">
    <td>...</td>
</tr>
ROW;
printf($tr, ($i++ % 2) ? "odd" : "even");


Was This Post Helpful? 1
  • +
  • -

#12 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: How much PHP, how much HTML

Posted 01 February 2011 - 02:58 AM

I agree completely with Dormilich. That way lies madness.
Was This Post Helpful? 0
  • +
  • -

#13 diego_pmc  Icon User is offline

  • D.I.C Addict

Reputation: 81
  • View blog
  • Posts: 565
  • Joined: 13-May 09

Re: How much PHP, how much HTML

Posted 01 February 2011 - 03:03 AM

It indeed is mixing HTML with PHP but it seemed to me that it didn't mess up the MVC pattern. The modelling (determining which page is the 'current' one and grabbing the table rows) still takes place in the scripts, and the viewing is just displaying the code.

I got the idea from gradbot@StackOverflow, who (to me it seemed) made a compelling case, so I went ahead with it.

I initially tried using tags (e.g.: TPL_CURRENT_PAGE) which the PHP script would then replace with actual code/content, but the HTML code got ugly really quick. It also seems to me that it's a lot more readable without those tags in the HTML code.

But, obviously, I'm willing to listen and change my approach, if needed. :)

This post has been edited by diego_pmc: 01 February 2011 - 03:04 AM

Was This Post Helpful? 0
  • +
  • -

#14 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3476
  • View blog
  • Posts: 10,007
  • Joined: 08-June 10

Re: How much PHP, how much HTML

Posted 01 February 2011 - 03:10 AM

View Postdiego_pmc, on 01 February 2011 - 11:03 AM, said:

It indeed is mixing HTML with PHP but it seemed to me that it didn't mess up the MVC pattern.

How is jumping in and out of a language not messing up the MVC? besides, the HTML code alone is plain unreadable due to the <?php ?> PIs (Processing Instructions). Any HTML designer would return that to you immediately.

if you grab table rows (resp. their data) from a database, there are other clever ways to do the output (cf. the fetch into class option of PDO and MySQLi)
Was This Post Helpful? 0
  • +
  • -

#15 diego_pmc  Icon User is offline

  • D.I.C Addict

Reputation: 81
  • View blog
  • Posts: 565
  • Joined: 13-May 09

Re: How much PHP, how much HTML

Posted 01 February 2011 - 03:18 AM

View PostDormilich, on 01 February 2011 - 03:10 AM, said:

How is jumping in and out of a language not messing up the MVC?

How is <section id="header"><?php $this->header->display() ?></section> interfering with MVC any more than <section id="header">TPL_HEADER</section> is, for example?

View PostDormilich, on 01 February 2011 - 03:10 AM, said:

if you grab table rows (resp. their data) from a database, there are other clever ways to do the output (cf. the fetch into class option of PDO and MySQLi)

I'll look into them. Guess this is an opportunity to implement PDO as well, huh? =))

This post has been edited by diego_pmc: 01 February 2011 - 03:20 AM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3