wendu Posted August 18, 2009 Share Posted August 18, 2009 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}; ?>)"> Quote Link to comment Share on other sites More sharing options...
rhodesa Posted August 18, 2009 Share Posted August 18, 2009 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 Quote Link to comment Share on other sites More sharing options...
wendu Posted August 18, 2009 Author Share Posted August 18, 2009 wow thanks! especially the loop thing will save me a lot of time and headaches 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.