Where should HTML formatting be used?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
inivux
Forum Newbie
Posts: 1
Joined: Thu Apr 12, 2012 7:19 pm
Location: San Diego

Where should HTML formatting be used?

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Where should HTML formatting be used?

Post 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.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Where should HTML formatting be used?

Post 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>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply