Jump to content

What's the best way to put HTML in PHP scripts?


geenee

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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'>&nbsp</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

Link to comment
Share on other sites

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'>&nbsp</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.

Link to comment
Share on other sites

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,

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.