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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:24:51+00:00 2026-06-18T10:24:51+00:00

I have the following piece of code and inside the function field_has_input(), I would

  • 0

I have the following piece of code and inside the function field_has_input(), I would like to assign the element’s key to the errors[]

i.e. errors[]=$key instead of errors[]=$field

The function field_has_input($field) gets called from check_all_fields($_POST)

Hopefully someone can help out.

function field_has_input($field)
{
    global $errors;

    if(!isset($field) || empty($field))
    {
        $errors[]=$field;
    }

}

function check_all_fields($_POST)
{
     global $errors;
    foreach($_POST as $key => $value)
    {
        echo $_POST[$key] . "<br />";
         field_has_input($_POST[$key]);

    }
    //exit;
    var_dump($errors);
    return $errors;
}
  • 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-18T10:24:52+00:00Added an answer on June 18, 2026 at 10:24 am

    I would suggest you dispense with the field_has_input() function entirely, and just do the work inside the other loop. Otherwise, you would need to be passing the $key into the inner function. In its current simplicity it is totally unnecessary and adds complexity.

    function check_all_fields($_POST)
    {
         global $errors;
        foreach($_POST as $key => $value)
        {
            // empty() calls isset() implicitly...
            if (empty($value)) {
              $errors[] = $key;
            }
        }
        //exit;
        var_dump($errors);
        return $errors;
    }
    

    Further, I would recommend not using the global errors, but instead passing it to the function. Because $_POST is a superglobal, it is unnecessary to pass it as a parameter to your function. Pass only $errors instead.

    function check_all_fields($errors) {
      //  $_POST is already available here...
      foreach($_POST as $key => $value)
      {
        if (empty($value)) {
          $errors[] = $key;
        }
      }
      return $errors;
    }
    

    If you must use an inner function, such as if the actual work to be done in field_has_input() is more complex than just checking empty(), pass the $key and check $_POST[$key] inside the function

    function field_has_input($key, $errors) {
      if (empty($_POST[$key])) {
        $errors[] = $key;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Inside a class, I have the following piece of code: /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Errors, typeof(ErrorsType))]
I have the following piece of code in my Model: public function getSite() {
I have the following piece of code, inside a php script foreach( $results as
I have a piece of html code like the following one: <td width=24%><b>Something</b></td> <td
I have an infinite scroll set up with the following piece of code. $(window).scroll(function
I have following piece of code: It compiles without problems under gcc-3.4, gcc-4.3, intel
I have following piece of code : public Hashmap<String,String> tempmap = new HashMap<String,String>(); and
I have the following piece of code that takes in some words, stores them
I have the following piece of code where i am concatenating events to the
I have the following piece of code and in the j for loop the

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.