Avatar ·

PHP Errors: Notice: Undefined variable, index, offset and how to fix them

Hello everyone! I'm a beginner in PHP and constantly encounter various Notices and Warnings when working with variables and arrays. For example, I have this code:

\n\n
<?php\n$user = $_POST['username'];\necho "Hello, " . $user;\n\n$data = array('name' => 'Ivan');\necho $data['age'];\n\n$items = array('apple', 'banana');\necho $items[5];\n\necho $undefined_var;\n?>
\n\n

And I get in the browser:

\n
    \n
  • Notice: Undefined variable: undefined_var
  • \n
  • Notice: Undefined index: username
  • \n
  • Warning: Undefined array key "age"
  • \n
  • Notice: Undefined offset: 5
  • \n
\n\n

The script works, but these messages spoil the output and look scary. I've read that you can disable display_error