0

I've this code:

<? if(is_home() || is_archive()): ?>
<ul>
  <li><a href="<?= site_url('/category/one') ?>">One</a></li>
  <li><a href="<?= site_url('/category/two') ?>">Two</a></li>
  <li><a href="<?= site_url('/category/three') ?>">Three</a></li>
</ul>
<? endif ?>

Which produces this output:

  <ul>
  <li><a href="http://domain/...">One</a></li>
  <li><a href="http://domain/...">Two</a></li>
  <li><a href="http://domain/...">Three</a></li>
</ul>

Notice that <ul> is indented. If I use the following it outputs as expected:

<? if(is_home() || is_archive()): ?><ul>
...

I know this is trivial but I'd like to know if there's a way to do this so it still looks nice in both PHP and HMTL; I like clean output.

5
  • 3
    PHP eats up a single newline after the closing token ?>. Why there are spaces before, no idea. Commented Feb 13, 2013 at 5:36
  • So I guess the only way is to put the opening tag on the same line or hit return twice, or am I missing something? Commented Feb 13, 2013 at 5:37
  • I feel like most things you can do to tidy up the output would mess up the actual code, which is far more important. Commented Feb 13, 2013 at 5:38
  • 3
    Personally I've given up on ironing t-shirts or caring about HTML indentation. Commented Feb 13, 2013 at 5:39
  • This question was more of a "why" and "is there a fix". The first comment seems to address this perfectly, I wasn't aware of that, I'll accept if posted as question @mario. I can figure out the alternatives now. Commented Feb 13, 2013 at 5:44

2 Answers 2

4

Okay. PHP eats up a single newline (\n or \r\n) after the closing token ?>.
As mentioned in http://php.net/manual/en/language.basic-syntax.instruction-separation.php

[...] The closing tag for the block will include the immediately trailing newline if one is present.

One workaround is also mentioned in the comments there. If you were to add a space after the ?>, meaning a sequence of ? > SPACE \n, the newline would retain its purpose. -- But then you'd depend on another invisible character to get the desired effect.

Sign up to request clarification or add additional context in comments.

Comments

2

firstly, why do you care if your html is formatted awkwardly? I understand perhaps for readability purposes but there are html formatting tools available: e.g. http://www.freeformatter.com/html-formatter.html

If you're concerned about you html code being valid, try using an appropriate IDE or an html validator: e.g. http://validator.w3.org/

lastly, the answer to your question: change the start of your code to this:

<?php if(is_home() || is_archive()){ echo "\n" ?><ul>
  <li><a href="<
...
<?php }?>

2 Comments

+1 for a valid solution but it doesn't help that much. I mean the PHP now looks uglier. I'll just leave the HTML as it is. It really doesn't matter, it was a question out of curiosity basically.
...ok. you can have your cake, but you can't eat it too ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.