Jump to content

Quick Question


heffe6

Recommended Posts

Hello everyone, I'm new here, but this seems like a great forum. 

 

I have a quick question that I haven't able to find a definitive answer to.  I'm trying to teach myself php, and I want to be able to write with best practices in mind.

 

Is it generally a better idea to create HTML with your php code like this?:

 

<?php
for($x=0;$x<5;$x++){
echo "<td>Number:</td>";
echo "<td>" . $x . "</td>";
}
?>

 

Or is it better to write the php around the html like this?:

 

<?php
     for($x=0;$x<5;$x++){
?> 
<td>Number:</td>
<td><?php echo $x ?></td>
<?php
     }
?>

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/147887-quick-question/
Share on other sites

It all depends on how large the "segments" are and how you feel easier coding.

 

Personally I'd code it like this:

<?php
for ($x=0;$x<5;++$x) {
  echo '<td>Number:</td>';
  echo '<td>'.$x.'</td>';
}
?>

 

But that's mainly because my text editor highlights all my code in different colors much like it has here.

Link to comment
https://forums.phpfreaks.com/topic/147887-quick-question/#findComment-776177
Share on other sites

Here are other ways you can code it...

 

<?php for ($x=0;$x<5;++$x) { ?>
  <td>Number:</td>
  <td><?=$x;?></td>
<?php } ?>
?>

 

<?php
for ($x=0;$x<5;++$x) {
  echo '<td>Number:</td><td>'.$x.'</td>';
}
?>

 

<?php
for ($x=0;$x<5;++$x) {
  echo '<td>Number:</td>
  <td>'.$x.'</td>';
}
?>

Link to comment
https://forums.phpfreaks.com/topic/147887-quick-question/#findComment-776194
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.