1
votes

You can see that I have a 3 element array. I am using a while loop to then print out all 3 values into a table, one row for each of the three values, but I do not understand how three rows are being printed out when I only have one row hard coded using html code. The PHP while loop does not echo the tr and td tags for each row because those row and detail tags are outside the PHP code. The code works -- it prints out one additional new table row for each value of "mary","donna","shirley", but I do not understand how. I could see it working if the tr and td tags were output by a PHP echo statement inside the while loop, but that is not the case here.

<html>
<body>
<table cellspacing ="2" cellpadding ="2" align ="center" border="8">
<?php  
$ar1=["mary","donna","shirley"];
$len=count($ar1);
$ct=0;
?>
<?php while ( $ct<$len) { ?>
<tr>
<td> 
<?php echo $ar1[$ct]; 
$ct++;
?> 
</td>
</tr> 
<?php } //end while loop?> 
</table>
</body>
</html>  
6
please provide the code. - user1759572
"The php while loop does not echo the tr and td " - er - yes it does. - user1864610
show me the echo statement that echoes the repeating tr and td tags - cryofan

6 Answers

0
votes

I believe the answer lies in the fact that HTML is an interpreted language not a compiled language.

So in this case your php while loop is setting the browser back to the spot just before your first tr tag so it goes through and interprets those tags again, and as it does this it puts them to the page again. Doing your while loop like this is a cheap way to do echos essentially.

I'm not a PHP master by any means, but from my understanding of how HTML is read and how PHP works this is my answer.

0
votes

Your <tr> tag is inside the while loop. If you want just one row, try this-

<html>
<body>
<table cellspacing ="2" cellpadding ="2" align ="center" border="8">
<?php  
$ar1=["mary","donna","shirley"];
$len=count($ar1);
$ct=0;
?>
<tr>
<?php while ( $ct<$len) { ?>
<td> 
<?php echo $ar1[$ct]; 
$ct++;
?> 
</td>
<?php } //end while loop?> 
</tr> 
</table>
</body>
</html>  
0
votes

This piece of code:

<?php while ( $ct<$len) { ?>
<tr>
<td> 
<?php echo $ar1[$ct]; 
$ct++;
?> 
</td>
</tr> 
<?php } //end while loop?> 

is the same as

<?php while ( $ct<$len) {
echo "<tr>
<td>";
echo $ar1[$ct]; 
$ct++;
echo "</td>
</tr>";
} //end while loop?> 
0
votes

if you analyze code more deeply..u will understand it yourself..you said "The php while loop does not echo the tr and td tags for each row because those row and detail tags are outside the php code" but it does ..the html code is not the part of php code it coded outside php scope...and whenever your while loop executes it again reads the tr and tg tag and insert row and hence u get three rows printed ...

0
votes

It's an easy case of PHP basic capabilities.

See http://php.net/manual/en/language.basic-syntax.phpmode.php

Everything outside of a pair of opening and closing tags is ignored by the PHP parser which allows PHP files to have mixed content.

PHP parser, don't need to know what is inside the While Loop, it just repeat to the output.

0
votes

In a php file, by escaping the php code (by way of ?> you basicly say to the script, now comes something non php, you provide Html tags which are then interpreted by the browser as html code.
But because you're still in the loop (you haven't ended it by adding an closing tag } you repeat the exit from the code, presenting html, and then entering the code again.

Some coders prefer to just exit php code and to show some html code with a few php tags here and there when there's a large amount of html being displayed.
It's a lot less typing than continually using echo statements.