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
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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