Page 1 of 4
					
				HTML within PHP
				Posted: Sun Jan 31, 2010 3:54 pm
				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.
 
			
					
				Re: HTML within PHP
				Posted: Sun Jan 31, 2010 4:02 pm
				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.
			 
			
					
				Re: HTML within PHP
				Posted: Sun Jan 31, 2010 4:45 pm
				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
			 
			
					
				Re: HTML within PHP
				Posted: Sun Jan 31, 2010 4:54 pm
				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 

 
			
					
				Re: HTML within PHP
				Posted: Sun Jan 31, 2010 5:46 pm
				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.
			 
			
					
				Re: HTML within PHP
				Posted: Sun Jan 31, 2010 6:20 pm
				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.
			 
			
					
				Re: HTML within PHP
				Posted: Mon Feb 01, 2010 5:23 pm
				by hallsofvallhalla
				but php is hidden from the view source which can come in handy at times.
			 
			
					
				Re: HTML within PHP
				Posted: Mon Feb 01, 2010 9:56 pm
				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.
 
			
					
				Re: HTML within PHP
				Posted: Fri Feb 05, 2010 1:48 pm
				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...
 
			
					
				Re: HTML within PHP
				Posted: Sun Feb 07, 2010 10:05 pm
				by hallsofvallhalla
				dont do that method for mmos though. You could really bog down the server with storing unneeded text.