-3

I got this code in php: my purpose of $errors is to display the error when I execute them in php. but sadly it says that $errors is not a defined variable.. how come? anybody could help me please?

if (empty($username) === true || empty($password) === true)
{
    $errors[] = 'You need to enter a username and password';
}

else
{
    //here
}

print_r ($errors);

}

and I got this error..

Notice: Undefined variable: error in C:\xampp\htdocs\shaven\login.php on line 28

May I know how to solved this?

1 Answers1

3

If your $username and $password isn't empty, then the code inside your if block won't be executed and $errors won't be defined. In that case, you'll just be trying to print_r() an undefined variable.

Initialize the $errors with an empty array before doing the comparison:

$errors = array();

And before doing print_r(), make sure the array isn't empty:

if (!empty($errors)) {
    print_r($errors);
}
Amal Murali
  • 75,622
  • 18
  • 128
  • 150