Jump to content

preferred way of coding with php?


wendu

Recommended Posts

hey

 

I was wondering if there's a de-facto standard way or best practice when coding php

 

should I make the whole document from startt to finish <?php ?> and then use echo everytime I need to print out html

 

or should I start and end php tags only when php is needed? I'm presuming that loops have to be inside php at all times, and output is handled by echoing?

 

can you post a sample of your code so I know how to start?

 

big thanks. I'm a still learner

 

edit: I mean is this a good way?

 

<?php

 

echo "<body onLoad=\"openSomething({$one})\">\n";

 

?>

 

instead of

 

 

<body onLoad="openSomething(<php? echo {$one}; ?>)">

 

Link to comment
https://forums.phpfreaks.com/topic/170870-preferred-way-of-coding-with-php/
Share on other sites

should I make the whole document from startt to finish <?php ?> and then use echo everytime I need to print out html or should I start and end php tags only when php is needed?

In general, I choose to use echo or stop/start php tags depending on the length and complexity of what I'm echoing. If it's heavy with static content, especially HTML tags and attributes, I'll stop/start the PHP tags. But if it's something simple, I just use an echo:

<?php
  $time = date('r');
  echo 'The time is: '.$time;
?>

 

I'm presuming that loops have to be inside php at all times, and output is handled by echoing?

Nope, the following is perfectly fine, and I use it all the time:

<?php
  while($row = mysql_fetch_assoc($result)){
?>
<tr>
  <td><?php echo $row['title']; ?></td>
  <td><?php echo $row['description']; ?></td>
</tr>
<?php
  }
?>

 

That said, for larger project, I have started using Smarty for most of my projects (especially big ones), and it allows me to separate my PHP logic from the HTML all together

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.