Henkka
Henkka

Reputation: 1663

Help with Undefined index and Undefined variable

Could somebody help me with this problem. I just trasferred my PHP/MYSQL program to another computer (both are running XAMPP and on localhost) and now I got a massive amount of Undefined index and Undefined variable ERRORS. Could somebody explain me why?

All the variables are checked with isset and all the values should be OK, but I can't get anything to work as it is supposed to and then there are the Undefined index and Undefined variable ERRORS.

Please Help! Thanks

Upvotes: 0

Views: 5632

Answers (4)

David Rodrigues
David Rodrigues

Reputation: 12532

Take a look here: error-reporting

You can try something like that (runtime):

<?php
  error_reporting(E_ALL ^ E_NOTICE);
?>

Or update php.ini:

error_reporting = E_ALL & ~E_NOTICE

Upvotes: 7

Maerlyn
Maerlyn

Reputation: 34107

The other answers help you fix the symptom, not the problem. You are not getting errors, just notices, it's a special error level that warns you of a possible problem, but does not stop execution.

Given php's nature of not needing to declare variables, it is very easy to make a spelling mistake in your code and spend yours tracking it down (I'm sure everyone has did it at least a few times). And there comes E_NOTICE to the rescue: it warns you whenever you're trying to use a variable that did not get set beforehand - helping you spot the typo.

Avoiding it is really easy, suppose you're checking the presence of a submit button in your post array to do form processing:

if ($_POST["submit"] == "submit")

in case it's a regular get request, that line will throw an E_NOTICE for "submit" being an invalid index in $_POST. Avoiding it is really easy:

if (isset($_POST["submit"]) && $_POST["submit"] == "submit")

this checks for the array index existence first. PHP uses lazy evaluation, in this case it means stopping after isset() if it returns false - the check that would throw the notice won't get executed. Also: isset is not a function, but a language construct - thus being fast.

My personal preference is to have error_reporting set to E_ALL | E_STRICT on all developer machines, that way I get notified of every possible problem - including using deprecated language features.

Upvotes: 2

dtbarne
dtbarne

Reputation: 8200

It sounds like your configuration on the first box had a lower error reporting level.

You can set that in php.ini or in your code via ini_set.

php.ini

error_reporting = E_ALL & ~E_NOTICE

php code

error_reporting(E_ALL ^ E_NOTICE);

Upvotes: 3

Alp
Alp

Reputation: 29739

Probably your error_reporting in the PHP configuration file (php.ini) is set differently as on your old XAMPP configuration.

Upvotes: 1

Related Questions