Indent PHP Echoes With HTML

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
Thomas-T
Forum Newbie
Posts: 7
Joined: Thu Jul 24, 2008 11:32 am

Indent PHP Echoes With HTML

Post by Thomas-T »

When I view the source code for my PHP, I've noticed that the PHP doesn't indent in the same way as my HTML. What I mean by that is I always indent my HTML with a tab, so that it's easier to see where everything is, and it just looks a lot neater. However, if I use PHP code in the middle of my HTML, the PHP code is not in-line with my HTML. In other words, the HTML generated by the PHP (as PHP isn't displayed in the source code) is always aligned left, and it looks very messy. I've discovered how to use line breaks in the PHP code, so that I don't have to scroll right to view all the code, but now I'm having a bit of trouble getting the PHP to indent with my HTML, so that everything looks nice and tidy. Does anyone have any suggestions? Would it be easier to not bother indenting the HTML, so that everything is aligned to the left?
mabwi
Forum Commoner
Posts: 27
Joined: Wed Aug 01, 2007 4:51 pm

Re: Indent PHP Echoes With HTML

Post by mabwi »

You can do this:

Code: Select all

 
  print "\n\t";
 
That will print a new line and then a tab.
manixrock
Forum Commoner
Posts: 45
Joined: Sun Jul 20, 2008 6:38 pm

Re: Indent PHP Echoes With HTML

Post by manixrock »

PHP only lines are not outputted at all.
You should always keep your html and php on different lines, so that the html lines contain their own tabs:

Code: Select all

<div>
<? if($someUglyVariable) { ?>
    <b>I HAVE A TABS BEFORE ME!!</b>
<? } ?>
</div>
Except small php insertion codes, or ugly but practical cases where you need the html on one line:

Code: Select all

And then he <? if($quote) { ?>said "<?=htmlspecialchars($quote)?>"<? } else { ?>said.. nothing.<? } ?>
Post Reply