galvin Posted October 23, 2009 Share Posted October 23, 2009 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 } ?> Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted October 23, 2009 Share Posted October 23, 2009 It only matters so far as what looks good to you and will make it easier to read in a year when you have to go back and make changes and additions. Quote Link to comment Share on other sites More sharing options...
keldorn Posted October 23, 2009 Share Posted October 23, 2009 My view on proper coding is to seperate the code from the template as much as possible. Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted October 23, 2009 Share Posted October 23, 2009 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 Quote Link to comment Share on other sites More sharing options...
keldorn Posted October 23, 2009 Share Posted October 23, 2009 I use Smarty for all my projects to achieve separating my code. 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. Quote Link to comment Share on other sites More sharing options...
trq Posted October 23, 2009 Share Posted October 23, 2009 I use Smarty for all my projects to achieve separating my code. 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. Quote Link to comment Share on other sites More sharing options...
Alex Posted October 23, 2009 Share Posted October 23, 2009 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). Quote Link to comment Share on other sites More sharing options...
keldorn Posted October 23, 2009 Share Posted October 23, 2009 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. 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. Quote Link to comment Share on other sites More sharing options...
PugJr Posted October 23, 2009 Share Posted October 23, 2009 Galvin, if you want to find out which is faster, this is one method. Loop both 10,000 times. Find out which processed faster but besides that, I don't know how you would check which is faster. Quote Link to comment Share on other sites More sharing options...
trq Posted October 23, 2009 Share Posted October 23, 2009 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.