116 Notice: Undefined index in php - wikitechy.com

Notice : Undefined index in php

Wikitechy | 7517 Views | php | 13 Jun 2016

 

Scenario:

When I try to execute the PHP script the following error will occur

“Notice: Undefined index: my index C:\wamp\www\mypath\index.php on line 11”

Line 45 and 30 looks like this: 

echo "Wikitechy index value is: " . $my_array["my_index"];

Reason:

<html> 
    <head> 
        <title>
array in php</title>
    </head>
<body> 

    <?php
        $data = array('WikitechyObject' => '100', 'php');
        echo $data['Bootstrap'];  
    ?> 
</body>
</html>

Here in this sample code the key is not defined in the array. 


    Here in this code we create an associative array whose name as “data” with its  key as 'WikitechyObject' & their values as '100', 'php.

    Here in this echo statement we print the value of the key “Bootstrap” but there is no available data’s in an array. 

Fix :

Here initially we use if condition for checking whether the key is available (or) not

  • For example, if the key is available it process the if section
  • Otherwise it moves to else section.

Observe the following PHP script:

<html>
    <head> 
        <title>array in php</title>
    </head>
<body> 
    <?php
        $data = array('WikitechyObject' => '100', 'php');
        if (array_key_exists('Bootstrap', $data)) {  
            //here we check the key value available (or) not
            echo $data['Bootstrap'];
            // available means print the Bootstrap key value	 
        }
        else {
            // Not available means executed this section
            echo 'No key Bootstrap in array';
        }
    ?>
</body>
</html>


    Here we check whether the array key value is existing (or) not using array_key_exists function, apart from that if the key value is available it prints the key value of the “Bootstrap” otherwise it prints “No Key Bootstrap in array”.

Applies to:

  • PHP 3 and 4
  • PHP 5
  • PHP 6 and Unicode
  • PHP 7

Related Tags:

  • PHP: "Notice: Undefined variable" and "Notice: Undefined index
  • How to fix 'Notice: Undefined index:' in PHP form action?
  • Undefined index in PHP
  • PHP - Notice: Undefined index
  • How to fix Notice: Undefined index in PHP form action?
  • PHP Notice: Undefined index: & PHP Notice: Undefined variable



Workshop

Bug Bounty
Webinar

Join our Community

Advertise
<