Re: Proper Way to Use [while] Loops



darkmojo wrote:
First of all, hello to everyone else on this newsgroup. I'm not sure
what the temperament of this community is when comes to newbs like me
but either way, nice to meet you all.

Second, I'm new to PHP and am trying to create a template from the
ground up. One of the things I would like to do is create newsposts
for the main part of my page that uses MySQL to hold the newspost and
set up a [while] loop to post every item on the newspost table.

Previously, I built something that worked pretty decently to do this
but I was a bit annoyed by having to use [echo] to create the HTML
template that would format the information from the newspost:

---[EXAMPLE: Simplified for time]---
<?php
...

$x = 1;
$postName = "Hello";
$postInfo = "blahblahblah";

while ($x <= 25) {
echo "<b>$postName</b><br />\n";
echo "<div class=\"post\">$postInfo</div>\n";
$x++;
}
?>
---[END]---

While this works great (and I realize I could simply use one instance
of [echo] instead of the two I have) I thought it was a bit messy, at
least from the perspective of a person that is used to working with
HTML and CSS and is new to scripting in general, as it is harder to
set up tab indentations with this method making the compiled code kind
of messy as well. It also requires me to escape all my quotations
which is extra annoying when you're working with a lot of CSS Classes
and IDs.

So I messed around a little bit and found out I could use this:

---[EXAMPLE: Simplified for time]---
<?php
...

$x = 1;
$postName = "Hello";
$postInfo = "blahblahblah";

while ($x <= 25) {
// START MARKUP
?>
<b>$postName</b><br />
<div class="post">$postInfo</div>
<?php
// END MARKUP
$x++;
}
?>
---[END]---

This seems to work equally well and in a more complicated template,
it's much easier to read and make changes as necessary. I'm just
wondering if somehow or another this is harmful coding? Is this setup
going to create problems of some sort now or down the road?

I'm building my site from scratch and while I have a basic idea of how
I want the end result to look, the server side programming part is new
to me and I'm learning as I go along. I guess I just wonder how the
PHP compiler sees this. So far, I've had no problems with this setup.
I'm just wondering if this is somehow bad practice.


http://v1.jeroenmulder.com/weblog/2005/04/php_single_and_double_quotes.php

....
This possibly isn’t anything new to you, but if you are working with HTML you can output valid markup while keeping your code readable. Using double quotes in a string enclosed with single quotes requires no escaping and vica versa. Using a double quote in a string enclosed in double quotes (idem for single quotes) does require escaping, which —in my opinion— degrades readability tremendously.
....

Mike

--- news://freenews.netfront.net/ - complaints: news@xxxxxxxxxxxx ---
.