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

The Archive Base Latest Questions

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

I wrote a foreach loop and saved on several lines of code. It basically

  • 0

I wrote a foreach loop and saved on several lines of code. It basically takes all of the $_POST variables and uses their names to create normal php variables.

foreach(array_keys($_POST) as $str)
{
${$str}=mysqli_real_escape_string($connection,trim($_POST["$str"]));
}

It is working as expected, creating variables dynamically.

Now, I wanted to put it inside a custom function so i modified it like this:

function createvariablesfromPOST()
{
    foreach(array_keys($_POST) as $str)
    {
    ${$str}=mysqli_real_escape_string($GLOBALS["connection"],trim($_POST["$str"]));
    }
    return //something;
}

Its not working obviously, because i dont know how to make this function return // something (whatever that thing may be) to the global scope. Whats supposed to be done here?

I cant make the foreach loop return anything, till the loop is complete. Isn’t that so?

Please help.

  • 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-29T05:25:50+00:00Added an answer on May 29, 2026 at 5:25 am

    Assigning arbitrary variables supplied by the user could be EXTREMELY DANGEROUS! They can overwrite any of your variables.

    The reason it isn’t working, is because they aren’t in the global scope. You would have to do something like $GLOBALS[$variable] = 'something';

    Instead, you should assign the variables to an array so they are isolated from the global scope. i.e. $input[$$var] = $escaped_value;

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

Sidebar

Related Questions

I wrote these lines: foreach (var catId in CatIds) { AdCategory.AdId = LastAd.AdID; AdCategory.CategoryId
Yesterday I wrote a piece of code to remove all the controls in a
I'm trying to see the output of a foreach loop in my code in
Is there a way to write this code more elegantly with a foreach loop?
I want to know how to replace a simple foreach loop with linq. I'm
A coworker used a for loop to iterate a List in some C# code
How can I loop through all the properties of object?. Right now I have
Hey all i have code to write to an xml doc from asp string
I'm writing some Scala code which uses the Apache POI API. I would like
I wrote a loop to display each line one by one from a List

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.