geenee Posted July 27, 2008 Share Posted July 27, 2008 Which is best? Method 1: <?php if ($condition == "true") { echo "<p>HTML code</p>"; } else { echo "<p>HTML code</p>"; } ?> Method 2: <?php if ($condition == "true") { ?> <p>HTML code</p> <?php } else { ?> <p>HTML code</p> <?php } ?> I have seen both used and am interestd to see if one is more efficient, or better programming practice. It looks like method 2 is the better one for large blocks of HTML when few, or no variables have to be called, and method 1 is better for short bits of HTML and/or when lots of variables are being used in the code, but what about large blocks of HTML where a quite a few variables or bits of PHP are going to be used?? Quote Link to comment https://forums.phpfreaks.com/topic/116831-whats-the-best-way-to-put-html-in-php-scripts/ Share on other sites More sharing options...
MFHJoe Posted July 27, 2008 Share Posted July 27, 2008 I'd say it's personal preference to be honest. I prefer method 1, and it's probably (marginally) quicker as PHP doesn't have to keep stopping and starting. You could alternatively use the heredoc syntax which was discussed quite a lot here yesterday. Quote Link to comment https://forums.phpfreaks.com/topic/116831-whats-the-best-way-to-put-html-in-php-scripts/#findComment-600761 Share on other sites More sharing options...
Entanio Posted July 27, 2008 Share Posted July 27, 2008 It all depends what your using it with. Quote Link to comment https://forums.phpfreaks.com/topic/116831-whats-the-best-way-to-put-html-in-php-scripts/#findComment-600763 Share on other sites More sharing options...
MasterACE14 Posted July 27, 2008 Share Posted July 27, 2008 or method 3.... the heredoc syntax. <?php if ($condition == "true") { echo <<<_HTML <p>HTML code</p> _HTML; } else { echo <<<_HTML <p>HTML code</p> _HTML; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/116831-whats-the-best-way-to-put-html-in-php-scripts/#findComment-600773 Share on other sites More sharing options...
matthewhaworth Posted July 27, 2008 Share Posted July 27, 2008 or method 3.... the heredoc syntax. <?php if ($condition == "true") { echo <<<_HTML <p>HTML code</p> _HTML; } else { echo <<<_HTML <p>HTML code</p> _HTML; } ?> Is that actually any good? I don't see at all in things like phpBB or Joomla coding.. but then again, they use a template system Quote Link to comment https://forums.phpfreaks.com/topic/116831-whats-the-best-way-to-put-html-in-php-scripts/#findComment-600780 Share on other sites More sharing options...
MFHJoe Posted July 27, 2008 Share Posted July 27, 2008 Is that actually any good? I don't see at all in things like phpBB or Joomla coding.. but then again, they use a template system It's no better/worse than any of the other methods really. They're all just used in different situations, but, ultimately, they all do the same thing. Quote Link to comment https://forums.phpfreaks.com/topic/116831-whats-the-best-way-to-put-html-in-php-scripts/#findComment-600787 Share on other sites More sharing options...
geenee Posted July 27, 2008 Author Share Posted July 27, 2008 I haven't come across heredoc much in scripts, I wonder what the advantages of it are? At the moment I'm using method 1, which is proving to be a bit annoying typing echo all the time, but then again closing php will mean displaying varibles takes longer to code, which doesn't matter if theres you haven't got many to display, but when some bits of your code have and some haven't it's a bit of a prediciment. Switching between the two methods is one option but I'm trying to keep all my code in the same format It's bits of code like this which is making me not be able to decide between which method I should use <?php echo "<div class='admin-property-box'>\n"; echo "<h1>$row[name]</h1><div style='clear:both'> </div>\n"; echo "<form action='$_SERVER[REQUEST_URI]' enctype='multipart/form-data' method='POST'>\n"; echo "<label for='name'>Property Name:</label> <input type='text' name='name' value=''><br />\n"; echo "<label for='name'>Price:</label> <input type='text' name='price' value=''><br />\n"; echo "<label for='date'>Date:</label>\n"; @include "form-date.inc"; echo "<br />"; echo "<label for='description'>Description:</label>\n"; echo "<textarea rows='5' cols='50' name='description'>\n"; echo "$row[description]\n"; echo "</textarea><br />\n"; echo "<br />Location:<br />\n"; foreach ($locations as $l_num => $l_name) { if ($l_num == $row[location]) { echo "<input type='radio' name='location' value='$l_num' checked='checked'>$l_name<br />\n"; } else { echo "<input type='radio' name='location' value='$l_num'>$l_name<br />\n"; } } echo "<br />Type of property: <br />"; foreach ($type as $t_num => $t_name) { if ($t_num == $row[type]) { echo "<input type='radio' name='type' value='$t_num' checked='checked'>$t_name<br />\n"; } else { echo "<input type='radio' name='type' value='$t_num'>$t_name<br />\n"; } } ?> The first bit has loads of echo's which i hate typing over and over, but then theres more variables in the second bit Quote Link to comment https://forums.phpfreaks.com/topic/116831-whats-the-best-way-to-put-html-in-php-scripts/#findComment-600792 Share on other sites More sharing options...
MFHJoe Posted July 27, 2008 Share Posted July 27, 2008 You don't have to type echo each line mate. Just do one. They can span multiple lines: echo "<div class='admin-property-box'>\n <h1>$row[name]</h1><div style='clear:both'> </div>\n <form action='$_SERVER[REQUEST_URI]' enctype='multipart/form-data' method='POST'>\n <label for='name'>Property Name:</label> <input type='text' name='name' value=''><br />\n <label for='name'>Price:</label> <input type='text' name='price' value=''><br />\n <label for='date'>Date:</label>\n"; @include "form-date.inc"; echo "<br /> <label for='description'>Description:</label>\n <textarea rows='5' cols='50' name='description'>\n $row[description]\n </textarea><br />\n <br />Location:<br />\n"; Works just as well. Quote Link to comment https://forums.phpfreaks.com/topic/116831-whats-the-best-way-to-put-html-in-php-scripts/#findComment-600803 Share on other sites More sharing options...
JasonLewis Posted July 27, 2008 Share Posted July 27, 2008 If you hate loads of echo statements, then heredoc syntax is for you. The only downside with heredoc syntax is that the ending of it cannot have any whitespace before it. If it's tabbed out, it'll throw you an error. So for tabbed coding it doesn't go to well. But it doesn't matter, you can always have the ending of it not tabbed, Quote Link to comment https://forums.phpfreaks.com/topic/116831-whats-the-best-way-to-put-html-in-php-scripts/#findComment-600807 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.