Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7430677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:10:46+00:00 2026-05-29T09:10:46+00:00

This is something that has always bothered me about PHP and I have never

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:10:47+00:00Added an answer on May 29, 2026 at 9:10 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is something that I have always wondered about, but never bothered to profile.
This is something that has always bothered me. Wouldnt it make more sense to
This is something that I have never fully grasped in .NET as to the
One of the things that has always bothered me about <jsp:include..> is that it's
This has always bothered me... Let's say I have an interface IFiddle and another
This has always bothered me, and I've never really come up with my own
Something that has always bothered me is doing more than one loop to manipulate
I saw this same question for VIM and it has been something that I
I have three models that look something like this: class Bucket < ActiveRecord::Base has_many
This is something that I always find a bit hard to explain to others:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.