When a notice occurs, the code may work, but it may not display what we expect.
I think that these are the three types of news that we can have more frequently in PHP.
Notice: Undefined variable
Notice: Undefined index
Notice: Undefined offset
What do these three news mean and what could generate them?
Note:
This question exists in SO in English , the idea is to have a good answer also in Spanish that helps to understand problems that PHP always shows us in English.
Notice: Undefined variable
occurs when you use a variable in an operation, but that variable has not been defined before. For example:Notice: Undefined offset
occurs when in an array you try to access a numeric index that does not exist in that array. For example:Notice: Undefined index
occurs when you try to access an alphanumeric index in an array that does not exist. A couple of examples:Variable: memory space that in php we reference with a name that begins with,
$
for example ,$edad
it could be a variable to store the age of a person.If an attempt is made to read (use) a variable before it has been defined, or is outside its scope of visibility, PHP returns "Undefined variable" .
Example where
$b
it has not been mentioned before set used:Example where
$b
it is being referenced outside its scope of visibility:By mentioning the variable, it is already defined, although in general this definition is accompanied by an initialization since if we define a variable it is logical that it has an initial value that makes sense. The solution is to define the variable before it is used and within the scope of visibility where it is needed example:
$b
before or after$a
:Index : is the String that is used as a key to reference a value in an associative array. In this example name and surname are Index of the associative array
$estudiante
:Undefined Index means that the string we are passing as Index when we want to read a value from the array does not exist. In the example the Index
domicilio
does not exist so$dom = $estudiante['domicilio'];
it returns Undefined Index .You can verify the existence of an Index with the function
array_key_exists($index);
Offset : is similar to Index but for common arrays where the index is an integer reflecting the position (or offset ) within the array. When trying to read the value of an array at a position that doesn't exist we get an Undefined Offset . Example:
One way to avoid these errors is to check that the index is greater than 0 and less than the number of elements in array (
count($arr)
);I thought it was with google translate. I use to check arrays, checking the key Example check cookies and if they are not set it:
if (!array_key_exists(COOKIE_ID,$_COOKIE)){Setcookie(COOKIE_ID, 'Y2l1ZGFkYW5v', time() + (3600),'','','', true);}
First of all a notice I would translate better as notice.
Notices are not errors . They are really warnings , and in this case it is simply warning you that these indices do not exist in the array.
To prevent these messages from appearing on the web, you have several options.
Remove in the PHP configuration that shows you the notice
Edit the php.ini and find the directive
error_reporting
Just above you will have the explanation of what it means exactly. In my case in production for example I have indicated this:
There I indicate that I want all the errors, but that I do not want the deprecated function warnings, nor the strict warnings, nor the warnings, nor the notices.
Disable notices in each php file
In each php file, you will have to execute this function so that it does not show you these errors in the web page generated by the php.
Program correctly detecting if it exists or not
In php there is a function called
isset()
that simply returns if the variable is defined and is not null.Instead of directly using an array with an index such as
$arr['indice']
, you can create a variable that, depending on whether or not the previous index exists in the array, has a value or not. That $index variable will no longer give a notice because it will always have a value.The above line of code is also equivalent to: