Skip to main content
New Participant
March 14, 2007
Question

How can HTML outside of php tags do this?

  • March 14, 2007
  • 3 replies
  • 291 views
Can someone explain to me how this segment of code works please? The part I don't understand is how the html tags inside the while loop are able to generate many rows when they are just once and not contained within php tags. I thought I would need something like echo "<tr>" or need to use printf function in order to write the html tags using php. Instead <tr> and <td> tags are not within the <?php /> tags so how can the table be dynamic and grow and shrink if php isn't writing the individual rows on each of the loops?

This topic has been closed for replies.

3 replies

cscampsteAuthor
New Participant
March 14, 2007
Oh that was my mistake. I thought everything had to be within <?php ?> tags and any html had to be echoed to the page. I see I still have a lot to learn :) Thanks for the explanation guys.
Inspiring
March 14, 2007
cscampste wrote:
> Can someone explain to me how this segment of code works please? The part I
> don't understand is how the html tags inside the while loop are able to
> generate many rows when they are just once and not contained within php tags. I
> thought I would need something like echo "<tr>" or need to use printf function
> in order to write the html tags using php. Instead <tr> and <td> tags are not
> within the <?php /> tags so how can the table be dynamic and grow and shrink if
> php isn't writing the individual rows on each of the loops?

It's a common misconception that everything has to be displayed using
echo. PHP is an embedded language. When the PHP engine sees an opening
PHP tag it starts parsing the PHP, when it sees a closing tag, it just
outputs the HTML.

The closing brace of the while loop is after the table row. It tells the
PHP engine to go back to the top of the loop and to continue doing so
until the condition is no longer true.

If you study the code generated by Dreamweaver for a repeat region,
you'll see that it uses the same principle. The only difference is that
Dreamweaver uses a do... while loop, rather than a simple while loop.

--
David Powers, Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Inspiring
March 14, 2007
In the section:

<?php while ($newArray = mysqli_fetch_array($res, MYSQLI_ASSOC)){?>
<tr>
<td><?php echo $newArray['fldFirstName']; ?></td>
<td><?php echo $newArray['fldLastName']; ?></td>
</tr>
<?php } ?>

PHP repeats everything between while { and } for as many records as are
retrieve by mysql_fetch_array. It doesn't matter that the HTML tags aren't
echoed by PHP, the PHP processor will still repeat everything inside the
while loop. In the end the whole page is a PHP page, so the whole page is
run through the PHP processor, not just the PHP tags, so its able to repeat
the HTML as many times as needed.

--
Gareth
http://www.phploginsuite.co.uk/
PHP Login Suite V2 - 34 Server Behaviors to build a complete Login system.