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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:58:05+00:00 2026-05-20T10:58:05+00:00

I thought I knew how to use arrays until I started storing form filling

  • 0

I thought I knew how to use arrays until I started storing form filling errors in them. So here is the situation: I want to declare an array at the beginning of my PHP document. Then throughout the document there is validation and at each validation the array is filled with an error if an error should be produced. Then at the end of the document I want to echo these errors into a specific on the page. So here is what I have now:

$errors = array();//declares array

if(/*some qualifier*/) {//username validation
} else {
$errors[] = "<p>Please enter a valid username</p>";
}

if(/*some qualifier*/) {//email validation
} else {
$errors[] = "<p>Please enter a valid email</p>";
}

echo '<div id="errors">';//errors div

foreach ($errors as $value) {//fills error div with the errors LINE 60
    echo "$value<br />\n";
}

echo '</div>';

So… what is wrong with that? I keep getting an error that errors is an undefined variable when it tries to echo the errors.

The error as given in the comments:

An error occurred in script ‘file path’ on line 160: Undefined variable: errors

Update: seems like its a problem with something weird in my code. If you feel like looking through 217 lines of code here is all the code: http://pastebin.com/YkERYpeF

  • 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-20T10:58:06+00:00Added an answer on May 20, 2026 at 10:58 am

    I have seen your code. You did only declare $errors inside a condition:

    //if the user has registered
    if (isset($_POST['submitted'])) {
    
    require_once (MYSQL); //gets the database connection
    
    $errors = array(); // declares the errors array that will be printed at end of validation if needed
    

    PHP arrays work great. You are declaring variables in a conditional scope and using them in a global scope. And PHP can’t imagine you want to use that variable in the global scope.

    You ought to indent your code as well, but you can perfectly define $errors just below $bodyId and PHP won’t complain anymore.

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

Sidebar

Related Questions

I thought I knew everything about UDTs and JDBC until someone on SO pointed
I thought that I knew how to use fast enumeration, but there is something
I thought I would post this here not so much as a question but
I've come across something in C++/CLI that goes against what I thought I knew:
So.. I thought I knew mssql fairly well but this one query asked of
I thought I knew how JavaScript's this keyword worked, but I've been caught by
I've thought I knew how to write basic regex. On my x64 pc in
A large amount of what I thought I knew about REST is apparently wrong
I thought I knew how this worked - obviously not. I'm not able to
Ok, I thought I knew what I was doing and am instead immensely confused

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.