"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
×


"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP

118933

Notice: Undefined Variable

A Variable is an identifier that denotes a storage location used to store a data value, In PHP variables are defined by "$" sign. When a developer not set a variable but try to use it in a PHP program it displays error.

For example:

<?php
$fruit = 'apple';
echo $ fruit;
echo $vegetable;
?>

Output:

Apple

| Notice: Undefined variable: vegetable in xyz.php on line no 4.

For resolving this error we can use isset() function in our PHP program, this function allows us to check the used variable is set or not so that we can resolve it.

(Note: the isset() function is supported by all earlier PHP versions.)


For example:

<?php
$fruit ='apple';
If(!isset($fruit)){
$fruit = "variable fruit is not set";
}
If(!isset($vegetable)){
$vegetable ="variable vegetable is not set";
}
echo $fruit;
echo $vegetable
?>

The more efficient way to resolve this, We can also use empty() function to check whether the variable is set or not, this function only returns the true or false for the variable. 

(Note: the empty() function also works with all earlier versions of PHP.)


For example:

<?php
$fruit =’apple’;
If(empty($fruit)){
$fruit = “variable fruit is not set”;
}
If(empty($vegetable)){
$vegetable =”variable vegetable is not set”;
}
echo $fruit;
echo $vegetable
?>

By using the above solution we can resolve the error undefined variable.


Notice: Undefined index, Warning: Undefined array key

The index of a value in an array is that value's location within the array. The Array key() is the is a built in function. When a User try to use the index of an array which is not defined it displays an error.

For example:

<?php
$fruit = (‘apple’, ‘banana’, ‘orange’, ‘mango’);
echo $fruit[0];
echo $fruit[1];
echo $fruit[2];
echo $fruit[3];
echo $fruit[4];
?>

Output:

Apple banana orange mango

| Notice: Undefined index in xyz.php on line no 7.

For resolving this error we can use isset() or also can use array_key_exist() function to check whether the index or array key is set or not.


For example:

<?php
$fruit = (‘apple’, ‘banana’, ‘orange’, ‘mango’);
if(!isset($fruit[4])){
$fruit[4] = “array index is not defined”;
}
echo $fruit[0];
echo $fruit[1];
echo $fruit[2];
echo $fruit[3];
echo $fruit[4];
?>

Another example:

<?php
$fruit = (‘apple’, ‘banana’, ‘orange’, ‘mango’);
if(!array_key_exist($fruit[4],  $fruit)){
$fruit[4] = “array key is not exist”;
}
echo $fruit[0];
echo $fruit[1];
echo $fruit[2];
echo $fruit[3];
echo $fruit[4];
?>

Notice: Undefined offset

when working with $_POST, $_GET or $_SESSION. We often faced an error such as:

| Notice: Undefined offset: at line 6. 

The solution to resolve such error is as follows:

For $_POST and $_GET we just have to check if the index exists or not before using them And for $_SESSION we have to start the session by using session_start() function and also check if the index exist or not.

Also we have to use superglobal variables and should be in UPPERCASE.



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments