0

This is something that has always bothered me about PHP and I have never found an answer. Hopefully this example will provide enough information to understand my question, as I don't know how to explain it thoroughly in words.

Here is some PHP inside of HTML, in a .php file. This type of things occurs pretty often:

<html>
    <head>
        <!-- whatever -->
    </head>
    <body>
        <section id="content">
            <ul class="errors">
            <?php
            foreach ($errors as $error)
            {
            ?>
                <li><?= $error ?></li>
            <?php
            }
            ?>

I have disregarded the end tags of the HTML and used <?= shortcuts to avoid any unnecessary text.

Now, here is what expect in the HTML output (i.e., right-click -> view source):

<html>
    <head>
        <!-- whatever -->
    </head>
    <body>
        <section id="content">
            <ul class="errors">
                <li>Example Error</li>

However, here is the actual result:

<html>
    <head>
        <!-- whatever -->
    </head>
    <body>
        <section id="content">
            <ul class="errors">
                             <li>Example Error</li>

Why? Because the foreach loop from PHP is indented 3 tabs in, so the <li> it is generating gets an ADDITIONAL 3 tabs. The solution I have been using? Here:

<html>
    <head>
        <!-- whatever -->
    </head>
    <body>
        <section id="content">
            <ul class="errors">
<?php
foreach ($errors as $error)
{
?>
                <li><?= $error ?></li>
<?php
}
?>

Sure, this works - but man is it ugly! It seems like I have two options: make my HTML output ugly and my code readable OR make my HTML readable and my code ugly. Why can't I have both? Is there no other way?

The best thing to do, I guess, is make the code readable and the output ugly. Who checks the source code anyway? I know this is probably the most popular reaction. Ugly HTML source code just bothers me, and I wish there was a way around it without sacrificing PHP code readability.

Logan Serman
  • 29,447
  • 27
  • 102
  • 141
  • take a look at source of pages like facebook.com or google.com ... does that html look pretty? :) I would ask a question: how can I minimize my html output size? I can answer your question: "Who checks the source code anyway"...well, you check it and work with it every day...on the other hadn, who checks html? Unimportant people who are not your users, but geeks like we and want to see how you made something :) – Aleksandar Vucetic Feb 05 '12 at 05:36

2 Answers2

2

php code doesn't generate any indents on the page. What's generating the indent is the indent in the

<li> line</li>

What you can do is: use echo to output it

 <?php
 foreach ($errors as $error)
        {

        echo "<li>$error</li>";

        }
        ?>

Or you could do:

  <?php
 foreach ($errors as $error)
    {?>

 <li><?=$error?></li> /(indent it in the begining as much as you want in the markup)

    <?}
    ?>

Although, imo, you needn't worry about how the markup looks. Its not important. Indents have no effect on the output

SoWhat
  • 5,564
  • 2
  • 28
  • 59
0

One solution would be to use the output buffer functions in PHP to buffer all HTML output, and then post-process it to indent everything properly.

See this other question on the topic:

How to properly indent PHP/HTML mixed code?

Community
  • 1
  • 1
Roadmaster
  • 5,297
  • 1
  • 23
  • 21