Even though we have been doing this for years, from time to time and right under our noses we come across Notice: Undefined index XXXX on line XXXX. A problem that is easy to solve but that comes from the excessive facilities that PHP gives us when programming.
Content
- Why am I getting an Undefined index or variable error message?
- To fix or not to fix Notice errors?
- Possible solutions to Notice Undefined index or variable errors
- 1. Solution to Notice: Undefined index or variable
- Fix Notice: Undefined index or Undefined Variable
- 2. Disable Notice errors
- Extra: Turn on developer-only bugs
Why am I getting an Undefined index or variable error message?
In the vast majority of compiled languages, not declaring variables and their type is usually a very serious error, for which our development would not even work. However, in PHP, an interpreted language that “doesn’t care” that we don’t define a variable and that changes their type is indifferent, is something common. So is it a contradiction that it shows us this PHP error? Not really, if we look closely it does not indicate an error in the strict sense, the message says notice, which means a warning, something like the Warning of other languages.
Let’s get to the point then, why does PHP notify me with this error? Well, basically it is a help to warn us of a possible problem in our programming, a clue for debugging our code in a language that lacks complete tools for debugging as if the IDEs of compiled languages have.
To fix or not to fix Notice errors?
You may be faced with the dilemma of whether or not to stop and fix the Notice. Although it is true that our code will work the same in most cases if we have programmed accordingly, it is not good practice to leave undefined variables here and there, especially if we think that sooner or later we will have to program in other languages susceptible to mistakes. Without going any further Javascript. In my opinion, the better our code and the better our strategies, even avoiding ads, however small they may be, the better programmers we will be.
Possible solutions to Notice Undefined index or variable errors
We can choose two strategies to address this common problem. In order of priority:
- Refine our code to avoid this error, getting used to taking it into account while developing.
- Ignore these errors while everything works and activate them only when errors appear that affect the proper functioning of our code.
1. Solution to Notice: Undefined index or variable
Solving these notifications will involve reviewing our code in the indicated lines, developing strategies to avoid them and in the future always programming according to them. I assure you that it is a matter of getting into the habit and in a short time your code will improve. You will avoid many errors produced by this cause!
Fix Notice: Undefined index or Undefined Variable
The Undefined index error occurs when trying to get the value of a key from an array that does not exist, while Undefined Variable refers to a variable that we have forgotten to define before using its value.
Both errors are very similar and the strategies to fix them are too. Let’s see the most common:
When to define or not variables
The first thing that comes to mind to solve these notifications is to declare all the variables that we are going to use in a script. Although this strategy would solve the problem in many cases, in others it is completely unnecessary. This error does not occur, for example, when we assign a new key or give a value to a variable, so for example the following code would be unnecessary:
<?php $array['pos1'] = ''; //AQUI HABRIA LINEAS DE CODIGO $array['pos1'] = $a + $b; //$a es 'HOLA ' y $b es 'MUNDO' ?>
As you see, declaring an array key that I am going to assign later is unnecessaryOn the other hand, if I am going to assign a value to this key within a possible IF, loop or function, it will avoid a Notice: Undefined index. For example:
<?php $array['pos1'] = 'Adiós'; if ( $a == 2 ) { $array['pos1'] = 'Hola mundo'; } echo $array['pos1']; ?>
In this example YES I have avoided the error, a failure that in my experience is quite common when we program in PHP.
Causes of Notice: Undefined index working with forms
Another very common case in which this error occurs is in the script that receives variables from a POST or GET form. The most recommended in this case is to avoid conditionals of the if type ( $_POST[‘input1’] ) even though we know that an undefined variable will evaluate to false, and instead use the PHP function intended for this purpose: isset ( mixed $var ).
The following code will produce a Notice even if it works correctly:
<?php if ( $_POST['submit'] ) { echo 'Bienvenido suscriptor a la mejor web de programación'; } ?>
The following code solves the same problem but avoids the Notice:
<?php if ( isset($_POST['submit']) ) { echo 'Bienvenido suscriptor a la mejor web de programación'; } ?>
Correct order of multiple conditions
A very common place where the Notice: Undefined index It is when we include several conditions, let’s imagine for example the following case:
<?php if ( $_POST['acepta_publicidad'] == 2 && isset ( $_POST['acepta_publicidad'] ) ) { echo 'Le mandamos publicidad'; } ?>
In this example, even though i make sure that i don’t show the message with two conditions, the order of these conditions is wrong. As the conditions linked with AND either && will only be true IF AND ONLY IF both hold, we should have placed the isset first. In this way if the key does not exist accept_advertising in the $_POST array, PHP will not check its value and we will avoid a Notice: Undefined index.
Conditions bound with AND or && will only be true IF AND ONLY IF both are true
2. Disable Notice errors
If we have decided to forget about Notice errors, either because we believe that our code works perfectly or because we focus on more serious errors, we can use the php function error_reporting ([ int $level ]).
PHP function error_reporting allows us to select the level of errors to display (E_WARNING, E_NOTICE, E_ERROR, E_PARSE). We can indicate the NO appearance of errors Notice in the following ways:
- Indicating all types, forgetting about the E_NOTICE level: error_reporting(E_ERROR | E_WARNING | E_PARSE)
- Indicating that we want to see all the types (E_ALL), except those of the level (E_NOTICE) by the symbol ^: error_reporting(E_ALL ^ E_NOTICE)
Extra: Turn on developer-only bugs
As a rule in our contracted Hosting the configuration file php.ini is not accessible and has error sampling disabledbut if for some reason this does not happen or we want to manipulate the configuration at will, we can use the php ini_set() function to modify the variable display_errors.
With the following example script we can make automatically show them when we develop locally and hide them remotely:
<?php if($_SERVER['REMOTE_ADDR']=="00.00.00.00") { ini_set('display_errors','On'); } else { ini_set('display_errors','Off'); } ?>