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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:53:53+00:00 2026-06-06T18:53:53+00:00

In PHP, what’s the best practice for dealing with a loop in a variable?

  • 0

In PHP, what’s the best practice for dealing with a loop in a variable? Let me explain.

I have a file called index.php which contains the following:

<?php 
$SELECT_INDUSTRY = array("Industry1", "Industry2", "Industry3");
require_once('form.inc');
?>
<html>
<head></head>
<body>
<?php echo $FORM_FIELD_INDUSTRY ?>
</body>
</html>

I then have a second file called form.inc:

<?php
$FORM_FIELD_INDUSTRY = '<select><option value=>Select an Industry</option>'.$INDUSTRY.'</select>';
?>

Here’s what is needed. I’d like to pass over the array values that are entered, into a foreach to process the array and spit out the <option></option> code. Here’s an example:

foreach ($SELECT_INDUSTRY as $INDUSTRY) {
echo '<option value="$INDUSTRY">$INDUSTRY</option>';
}

So this function would build my select options based on the array values. This works, but how would I be able to then pass the total values into a variable that I’ve defined in the form.inc file above? In the form.inc file, I have $INDUSTRY as the variable, which I suppose needs some sort of return from the other function. This may also need global variables to pass between functions if it comes to it. Is there a better way of handling this?

My end goal is to have the variable $FORM_FIELD_INDUSTRY which spits out the foreach function based on the array values.

SOLUTION

The solution is listed below, but for anyone coming across this, here is what I did:

index.php

<?php 
$SELECT_INDUSTRY = array("Industry1", "Industry2", "Industry3");
$FORM_SELECT_SIZE = ''; 
require_once('form.inc');
?>
<html>
<head></head>
<body>
<?php get_options( $FORM_FIELD_INDUSTRY ) ?>
</body>
</html>

form.inc

<?php
function get_options( $arr = array() ) {
    global $FORM_SELECT_SIZE;
    echo '<select id="industry" class="'.$FORM_SELECT_SIZE.'"><option value=>Select an Industry</option>';
    foreach( $arr as $option ) {
        echo '<option>'.$option.'</option>';
    }
    echo '</select></div></div>';
}
$FORM_FIELD_INDUSTRY = $SELECT_INDUSTRY;
?>

Pretty simple solution for passing values between the function, and variables.

  • 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-06-06T18:53:54+00:00Added an answer on June 6, 2026 at 6:53 pm

    You could just use a function to build the options and return it like so:

    function get_options( $arr = array() ) {
        echo '<select>';
        foreach( $arr as $option ) {
            echo '<option>'.$option.'</option>';
        }
        echo '</select>';
    }
    

    And then in your HTML you could call it like this:

    <html>
    <head></head>
    <body>
    <?php get_options( $SELECT_INDUSTRY ); ?>
    </body>
    </html>
    

    So if I were to take the code you gave us on top and combined it all it would look like this:

    <?php 
    $SELECT_INDUSTRY = array("Industry1", "Industry2", "Industry3");
    require_once('form.inc');
    ?>
    <html>
    <head></head>
    <body>
    <?php get_options( $SELECT_INDUSTRY ); ?>
    </body>
    </html>
    <?php
    function get_options( $arr = array() ) {
        echo '<select>';
        foreach( $arr as $option ) {
            echo '<option>'.$option.'</option>';
        }
        echo '</select>';
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
PHP: I have made up a function that returns an array. I would like
PHP 5.2.15 I am trying to replace {date[F]} with the date function. I have
----- PHP and mySQL ----- I have two quick questions need some advice. On
this is what i have right now Drawing an RSS feed into the php,
PHP's documentation on this function is a bit sparse and I have read that
PHP script $count = 0; foreach ($_FILES['filesToUpload'] as $file) { //upload process echo $file[$count]['tmp_name'].',';
PHP supports variable interpolation in double quoted strings, for example, $s = foo $bar;
PHP's sha256 outputs a string which is 64 chars long because it's base 16.
PHP has a var_dump() function which outputs the internal contents of an object, showing

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.