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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:10:46+00:00 2026-06-01T03:10:46+00:00

So this all stemmed from not wanting to check each POST in a huge

  • 0

So this all stemmed from not wanting to check each POST in a huge If statement with tons of ANDs like so:

if(isset($_POST[$question], $_POST[$choice1], $_POST[$choice2], $_POST[$choice3], $_POST[$choice4], $_POST[$choice5], $_POST[$password]) && 
    strlen(question > 0) &&
    strlen(choice1 > 0) &&
    strlen(choice2 > 0) &&
    strlen(choice3 > 0) &&
    strlen(choice4 > 0) &&
    strlen(choice5 > 0) &&
    strlen(password > 0)) 
{
    $cat=$_POST['cat'];
    $question=$_POST['question'];
    $choice1=$_POST['choice1'];
    $choice2=$_POST['choice2'];
    $choice3=$_POST['choice3'];
    $choice4=$_POST['choice4'];
    $choice5=$_POST['choice5'];
    $password=$_POST['password'];
}
else{
    //Incorrect password or missing data
}

Instead I created an array with a string for each variable I wanted to check for and a foreach loop to run my tests

//create array with all wanted variables
$variables = array('cat', 'question', 'choice1', 'choice2', 'choice3', 'choice4', 'choice5', 'password');
$returnError = FALSE;
//run through array to check that all have been posted and meet requirements
foreach($variables as $var) {
    if(!isset($_POST[$var]) || !strlen($_POST[$var]) > 0) {
        $returnError = TRUE;
        break;
    }
}

Now I want to take the string names and instantiate variables with the same name

//create array with all wanted variables
$variables = array('cat', 'question', 'choice1', 'choice2', 'choice3', 'choice4', 'choice5', 'password');
$returnError = FALSE;
//run through array to check that all have been posted and meet requirements
foreach($variables as $var) {
    if(!isset($_POST[$var]) || !strlen($_POST[$var]) > 0) {
        $returnError = TRUE;
        break;
    }
    else{
        global $*/variable named $var*/ = $_POST[$var];
    }
}

Is that possible? If not, how would I do something similar with an array of variables like so:

$variables = array($cat,$question,$choice1,$choice2,$choice3,$choice4,$choice5,$password);
  • 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-01T03:10:47+00:00Added an answer on June 1, 2026 at 3:10 am

    You can use Variable variables, although it is not recommended.

    global $$var = $_POST[$var];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I been at this all day and feel like I'm getting nowhere, I need
I see code like this all over the web var days= Monday Tuesday Wednesday
For an access database that looks like this: (All text fields) Co1 Co2 Co3
I have been working on this all day, much of my code is from
I think this all comes down to a matter of syntax. I am not
I run into similar codes like this all the time in aspx pages: <asp:CheckBox
This all worked fine in rails 2.3.5, but when a contractor firm upgraded directly
sorry if this all seem nooby and unclear, but I'm currently learning Netlogo to
I've struggled with this all day, I am trying to get a random number
I see this all the time: object literals declared such that some keys are

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.