Page 1 of 1

Where should HTML formatting be used?

Posted: Thu Apr 12, 2012 7:40 pm
by inivux
Newbie to PHP here. I am well versed in HTML and CSS, but I have a simple question about where to use HTML formatting, when writing PHP.

When using a simple string variable in an echo statement, should I:
  1. Format the string in the definition of the variable, or;
  2. Format the variable in the echo statement (which will ultimately display the value of the variable)?
Which one is more elegant or preferred by PHP programmers? Does it depend on the circumstance? Is one faster than the other?

In other words, should I use the below:

Code: Select all

$variable = "This is an example.";

echo "<h1>$variable</h1>
Or this:

Code: Select all

$variable = "<h1>This is an example.</h1>";

echo "$variable"
I apologize if the semantics of my question are confusing; I tried to make it as clear as possible.

Thank you in advance for any help.

-Matthew

Re: Where should HTML formatting be used?

Posted: Thu Apr 12, 2012 8:51 pm
by requinix
Depends. Is $variable the title that you want to use in whatever (#1) or is it some HTML you want to output (#2)?

Above all, pick whatever makes sense to you and pick it consistently.

Re: Where should HTML formatting be used?

Posted: Fri Apr 13, 2012 12:54 am
by social_experiment
inivux wrote:Which one is more elegant or preferred by PHP programmers?
I've seen this format in a few places

Code: Select all

<h1><?php echo $variable; ?></h1>