Jump to content

[SOLVED] General question about proper coding...


Recommended Posts

Maybe it doesn't matter either way, but is one of these methods more proper/efficient than the other?

 

Basically, I think the 1st way is easier because you don't have to ECHO everything, but maybe it's bad practive to keep "stopping and starting" PHP???  Anyway, let me know if either way is more "proper" or if it simply doesn't matter.

 

<?php
if ($_SESSION['dob'] == "on") {
?>
<tr>
<td class="bold">Date of Birth: </td>
</tr>
<?php
} else {
// do nothing
}
?>


OR

<?php
if ($_SESSION['dob'] == "on") {
echo "<tr><td class='bold'>Date of Birth: </td></tr>";
} else {
// do nothing
}
?>

If you are going to be distributing your code, then I would probably try to seperate your PHP and HTML, as a bunch of different opening and closing tags can make your code kind of confusing. However, if you are coding your own projects, than I would open/close your PHP tags as much as you want.

 

Doing it too much can become confusing, but with a proper balance, you should be fine

I use Smarty for all my projects to achieve separating my code.  :D

To be honest I didn't like Smarty when I first starting learning programming. I thought it was like a cryptic language to learn. But I think now people who say that are just noobies to begin with. Its really friggen easy to use.

I use Smarty for all my projects to achieve separating my code.  :D

To be honest I didn't like Smarty when I first starting learning programming. I thought it was like a cryptic language to learn. But I think now people who say that are just noobies to begin with. Its really friggen easy to use.

 

Using a template engine doesn't necessarily separate your business logic from the template any more than writing good code does in the first place. All it does is adds overhead and another syntax to learn.

If you're wanting to completely separate your HTML from your PHP and you want to take the next step I'd suggest studying MVC. There are many good Frameworks out there that implement this architectural pattern. Things like CodeIgniter, Zend Framework, etc.. It's not something you can familiarize yourself with overnight, but it's definitely worth it in the long run (Both using a Framework and learning MVC).

Yes but when we say seperate the code, what does that really mean? To me it means removing the mysql database connection and  creating arrays, and assigning variables, all that stuff. In the template the most I have is just {$var}  and maby a loop if I am generating a bullet list or something like that from an array.

It also has a great caching feature which lends itself to be very useful in cutting down your mysql queries.

These days in my code you wont ever catch a html tag in in it. I make good effort to never put put html in my php code unless absolutely necessary. :)

 

 

 

I use Smarty for all my projects to achieve separating my code.  :D

To be honest I didn't like Smarty when I first starting learning programming. I thought it was like a cryptic language to learn. But I think now people who say that are just noobies to begin with. Its really friggen easy to use.

 

Using a template engine doesn't necessarily separate your business logic from the template any more than writing good code does in the first place. All it does is adds overhead and another syntax to learn.

To me it means removing the mysql database connection and  creating arrays, and assigning variables, all that stuff. In the template the most I have is just {$var}  and maby a loop if I am generating a bullet list or something like that from an array.

 

That exact same logic can be easily achieved without a template engine, overhead or foreign syntax.

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.