HTML within PHP

C++, C#, Java, PHP, ect...
Animatro
Posts: 9
Joined: Sat Jan 30, 2010 9:13 pm

HTML within PHP

Post by Animatro »

Sorry if something like this has been posted beforehand, I'm just curious to see how you guys handle it.

Do you escape php, type your html then enter php again?

Code: Select all

<?php
$VariableName = 0;
if ($VariableName == 0) {
          ?>
          <img src = "picture.jpg">
          <?php
}
Or do you just echo your html within php?

Code: Select all

<?php
$VariableName = 0;
if ($VariableName == 0) {
          echo '<img src = "picture.jpg">';
}
I assume escaping php and then enterring your html would be the faster method, whereas echo'ing it seems easier to read to me. Personal preference of course.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: HTML within PHP

Post by Jackolantern »

It really depends on the situation. For just a simple link, or a bit of text, echo'ing the HTML is fine. However, it gets to the point where it becomes harder and harder to read as you try to echo things like forms, tables, etc. In that case, it is best to close PHP tags and write it in standard HTML.
The indelible lord of tl;dr
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: HTML within PHP

Post by hallsofvallhalla »

I use php to echo out all my html, even my forms. It just makes it easier for me to check my code and keep it all one standard
User avatar
OldRod
Posts: 1320
Joined: Sun Sep 20, 2009 4:26 pm

Re: HTML within PHP

Post by OldRod »

I sort of mix and match... I usually echo out HTML if it's just a few lines, especially if it's inside a while loop or an if/else. But if I'm doing a big nested table I'll usually go pure HTML just to eliminate all the extra " and ; which get confusing when echo'ing it :)
Animatro
Posts: 9
Joined: Sat Jan 30, 2010 9:13 pm

Re: HTML within PHP

Post by Animatro »

Yeah. Personally, if it's inside a loop or if statement I usually just echo it out.

But if it's a large block of html it's probably easier to exit php. I hate having to escape all the quotations and things like that, very annoying. I was just curious because I've been trying to clean up my php lately and make things a lot easier to read and a lot easier to edit later.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: HTML within PHP

Post by Jackolantern »

And while PHP is rarely ever a performance consideration compared to SQL queries, it is more efficient to submit pure HTML instead of PHP echo'd HTML because the PHP parser does not have to register and generate it.
The indelible lord of tl;dr
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: HTML within PHP

Post by hallsofvallhalla »

but php is hidden from the view source which can come in handy at times.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: HTML within PHP

Post by Jackolantern »

hallsofvallhalla wrote:but php is hidden from the view source which can come in handy at times.
Not exactly sure what you mean. Whether you echo() the html, or type it directly, only the html is sent to the browser. The output will look identical if a viewer looks at the page source in their browser.
The indelible lord of tl;dr
User avatar
MAruz
Posts: 117
Joined: Fri Nov 20, 2009 12:31 pm

Re: HTML within PHP

Post by MAruz »

A friend of mine showed me a third method:

Code: Select all

$output1 = <<< OUTPUT
    // Write plain HTML code here (or anything else you simply want printed), you can also use PHP variables here
OUTPUT;
echo $output1;
This way you can write lots of HTML, and keep it neat and tidy, while also using variables to get a more dynamic structure.
You need to have the closing OUTPUT; tag hit the left margin of your editor (E.G. no whitespaces or other symbols to the left of this tag).
And you cannot use anything but clean variables ($array['something'] would not work).

I don't bother doing this unless I have a few HTML lines to write, and/or I want to use php variables within the HTML.
So just a simple link I just close and open php tags as described above...
PHP, Java, JavaScript, HTML, CSS, XML, MySQL / Oracle
Photoshop, Illustrator
www.cuddly-zombie.com
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: HTML within PHP

Post by hallsofvallhalla »

dont do that method for mmos though. You could really bog down the server with storing unneeded text.
Post Reply

Return to “Coding”