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

  • Home
  • SEARCH
  • 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 6563223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:48:16+00:00 2026-05-25T13:48:16+00:00

I have some PHP code like the following (simplified): <ul> <?php for ($Index =

  • 0

I have some PHP code like the following (simplified):

<ul>
    <?php
        for ($Index = 1; $Index <= 10; $Index++)
        {
            echo("<li>" . $Index . "</li>\n");
        }
    ?>
</ul>

The problem is that for all lines after the first, the output is without indentation. I want to keep my code neat, so I’d like all the <li> elements to be aligned properly.

I tried outputting tabs before each element with \t, but then the first line is indented more than intended. Outputting the tab after the element means the trailing </ul>‘s placement will be messed up.

\r does not work at all.

Are there any tricks to keeping output properly formatted, or do I have to live with messy code?

  • 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-25T13:48:17+00:00Added an answer on May 25, 2026 at 1:48 pm

    The first line is more indented than you want it to be because of the extra whitespace at the beginning of this line:

        <?php
    

    Since that white space is outside the PHP tags, it is output directly. But because it is outside the PHP tags, it is not included in the loop, and will only affect the first line.

    You could do this to help avoid it:

    <ul>
    <?php
      for ($Index = 1; $Index <= 10; $Index++) {
        echo "\n    <li>$Index</li>";
      }
    ?>
    </ul>
    

    …and align the <?php ?> tags at the beginning of the lines, or you could do this:

    <ul><?php for ($Index = 1; $Index <= 10; $Index++) { ?>
        <li><?php echo $Index; ?></li>
    <?php } ?></ul>
    

    …but as ridiculous as it seems, this:

    <ul><?php for ($Index = 1; $Index <= 10; $Index++) echo "\n    <li>$Index</li>"; ?>
    </ul>
    

    …is probably the best way to acheive what you are talking about leaving the least room for ambiguity (different PHP versions seem to handle this slightly differently – for instance in PHP 4.3.10 at least, there is an implicit line break after a ?> tag which does not exist in PHP 5 (I think this may have been a bug). That is one of the reasons I don’t use mixed HTML and PHP (although I know many people disagree with me on this point) but what I would rather do is this:

    <?php
    
      $out = "<ul>\n";
      for ($Index = 1; $Index <= 10; $Index++) $out .= "    <li>$Index</li>\n";
      $out .= "</ul>";
      echo $out;
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have some PHP code that looks like: $message = 'Here is the
In PHP, say that you have some code like this: $infrastructure = mt_rand(0,100); if
Let's say I have some code like this: <html> <head><title>Title</title></head> <body> <?php if (!$someCondition){
I have some PHP code that generates dynamic tables of data on the fly.
I have some PHP code that is designed to make a spreadsheet with formulas
I have some PHP code that generates a calendar and then outputs html to
I have a section of code like the following: ---- file.php ---- require_once(mylib.php); function($a,$b)
I have something like the following code in one of my views: <?php $this->load->view('_validation_error',
I am facing a strange problem. My PHP page has following code. echo <div
I have some PHP code which allows me to sort a column into ascending

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.