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 928377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:59:57+00:00 2026-05-15T19:59:57+00:00

Suppose I want to create a select box in PHP. $months = array (‘Jan’,’Feb’,’Mar’,’Apr’,’May’,’Jun’,’Jul’,’Aug’,’Sep’,’Oct’,’Nov’,’Dec’);

  • 0

Suppose I want to create a select box in PHP.

$months = array ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

Option 1:

<select name="month">
    <option value="">Month</option>
    <option value="">-------</option>
    <?php foreach ($months as $monthNum => $month) { ?>
    <option value="<?php echo $monthNum+1 ?>"><?php echo $month ?></option>
    <?php } ?>
</select>

Option 2:

<select name="month">
    <option value="">Month</option>
    <option value="">-------</option>
    <?php 
        foreach (months() as $monthNum => $month) 
            echo '<option value="' . ($monthNum+1) . '">' . $month . "</option>\n"; 
?>
</select>

I personally think Option 1 renders the code harder to read and understand, at the same time Option 2 screws the indentation and puts more HTML into PHP.

Which option is preferred?

EDIT: I don’t want to add another template engine on top of PHP, which is itself a template engine.

  • 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-15T19:59:58+00:00Added an answer on May 15, 2026 at 7:59 pm

    I think the second option is better because things like <option value="<?php echo $monthNum+1 ?>"> simply are hard to read and understand and imho even a little bit illogical (a tag in a tag.)

    But honestly, use what you like more. Only one suggestions: Maybe instead of using <?php foreach () { ?> ... <?php } ?> which is sometimes hard to understand you may use this syntax: <?php foreach (): ?> ... <?php endforeach; ?>. Especially if you start having foreaches in fors in whiles in foreaches in fors this syntax may be a little bit easier to understand.

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

Sidebar

Related Questions

I want to create sub-domains using PHP on the fly. Suppose a user registers
Suppose I want to get student summaries from a two tables: student, grade: CREATE
Let's suppose that I have a list of elements, and I want to select
Suppose I want to add two buffers and store the result. Both buffers are
Suppose I want to store N samples (each sample takes up a significant portion
Suppose I want to have a blog with Rails 3 on my website and
Suppose I want to put objects that identify a server into a stl set
Suppose I want to execute code, for example value += 5 inside a namespace
Suppose I want to make a Web application which uses a fixed width and
Suppose you want to dynamically add to a HTML menu, where the menu HTML

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.